English 中文(简体)
Rails:未定义方法to_sym
原标题:Rails: Undefined method to_sym

我在部署的Rails 2.3.5应用程序中遇到以下错误:

NoMethodError(未定义方法“to_sym”对于nil:NilClass):

我的本地测试应用程序安装使用的是Sqlite,没有出现错误,但是我的部署应用程序运行Mysql时出现了错误。两者之间唯一的区别是我的本地机器上运行Ruby 1.8.7,而部署服务器上运行1.8.6。

I ve 包括以下代码:User.rb和错误标识。 我是继申报性授权和假装授权铁路预报之后设立的。


编辑:这是application_controller的代码,我在其中使用before_filter设置current_user

class ApplicationController < ActionController::Base
  helper :all
  helper_method :current_user_session, :current_user 
  before_filter :set_current_user

  protected 

  def set_current_user
    Authorization.current_user = current_user
  end

  def current_user_session  
    return @current_user_session if defined?(@current_user_session)  
    @current_user_session = UserSession.find  
  end  

  def current_user  
    @current_user = current_user_session && current_user_session.record  
  end
end

把这个翻译成中文:--

用户b:

class User < ActiveRecord::Base
  acts_as_authentic 

  has_many :products
  has_many :transactions

  ROLES = %w[admin dmstaff staff faculty]

  def role_symbols
    [role.to_sym]
  end  

end

错误日志:

NoMethodError(未定义方法“to_sym”对于nil:NilClass):
  app/models/user.rb:10:in `role_symbols 
  /usr/lib/ruby/gems/1.8/gems/declarative_authorization 0.4/lib/declarative_authorization/authorization.rb:242:in `roles_for 
  /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:296:in  `user_roles_privleges_from_options 
  /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/authorization.rb:161:in `permit! 
  /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/in_controller.rb:580:in `permit! 
  /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/in_controller.rb:109:in `filter_access_filter 
  /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/in_controller.rb:109:in `each 
  /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/in_controller.rb:109:in `all? 
  /usr/lib/ruby/gems/1.8/gems/declarative_authorization-0.4/lib/declarative_authorization/in_controller.rb:109:in `filter_access_filter 
  passenger (2.2.5) lib/phusion_passenger/rack/request_handler.rb:95:in `process_request 
  passenger (2.2.5) lib/phusion_passenger/abstract_request_handler.rb:207:in `main_loop 
  passenger (2.2.5) lib/phusion_passenger/railz/application_spawner.rb:378:in `start_request_handler 
  passenger (2.2.5) lib/phusion_passenger/railz/application_spawner.rb:336:in `handle_spawn_application 
  passenger (2.2.5) lib/phusion_passenger/utils.rb:183:in `safe_fork 
  passenger (2.2.5) lib/phusion_passenger/railz/application_spawner.rb:334:in `handle_spawn_application 
  passenger (2.2.5) lib/phusion_passenger/abstract_server.rb:352:in `__send__ 
  passenger (2.2.5) lib/phusion_passenger/abstract_server.rb:352:in `main_loop 
  passenger (2.2.5) lib/phusion_passenger/abstract_server.rb:196:in `start_synchronously 
  passenger (2.2.5) lib/phusion_passenger/abstract_server.rb:163:in `start 
  passenger (2.2.5) lib/phusion_passenger/railz/application_spawner.rb:213:in `start 
  passenger (2.2.5) lib/phusion_passenger/spawn_manager.rb:262:in `spawn_rails_application 
  passenger (2.2.5) lib/phusion_passenger/abstract_server_collection.rb:126:in `lookup_or_add 
  passenger (2.2.5) lib/phusion_passenger/spawn_manager.rb:256:in `spawn_rails_application 
  passenger (2.2.5) lib/phusion_passenger/abstract_server_collection.rb:80:in `synchronize 
  passenger (2.2.5) lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize 
  passenger (2.2.5) lib/phusion_passenger/spawn_manager.rb:255:in `spawn_rails_application 
  passenger (2.2.5) lib/phusion_passenger/spawn_manager.rb:154:in `spawn_application 
  passenger (2.2.5) lib/phusion_passenger/spawn_manager.rb:287:in `handle_spawn_application 
  passenger (2.2.5) lib/phusion_passenger/abstract_server.rb:352:in `__send__ 
  passenger (2.2.5) lib/phusion_passenger/abstract_server.rb:352:in `main_loop 
  passenger (2.2.5) lib/phusion_passenger/abstract_server.rb:196:in `start_synchronously 

Rendering /var/data/app/current/public/500.html (500 Internal Server Error)
最佳回答
  def role_symbols
    [role.to_sym]
  end

roleNil。你有定义 role 吗?

问题回答

在某个时刻,你的代码期望一个类型为User的对象,但现在却是nil。你是否有做过类似的事情?

@user = User.find_by_login("Mary")
@user.role_symbols

在哪裡,“Mary”是不存在的用戶登錄?發布任何您調用role_symbols方法的地方,我們可以提供更多幫助。

编辑:看一下这个方法#roles_for(链接),没有深入研究这个插件,我想说的是,在您的代码执行这一点上,#current_user没有设置。

表格上没有设置角色属性。您必须使用救援代码,并检查为什么会发生这种情况。

在调用 role.to_sym 之前,测试 role 值是否已设置。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签