Rails:未定义的方法'to_sym'

时间:2010-02-15 16:05:32

标签: ruby-on-rails ruby roles declarative-authorization

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

NoMethodError(未定义的方法`to_sym'代表nil:NilClass):

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

我已经包含了User.rb中的代码和下面的错误日志。我在声明授权和嵌入式授权Railscast之后进行了设置。

非常感谢任何帮助!

编辑:这是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

-

User.rb:

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 (undefined method `to_sym' for 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)

3 个答案:

答案 0 :(得分:5)

  def role_symbols
    [role.to_sym]
  end

- &GT; roleNil。你在某处定义role吗?

答案 1 :(得分:1)

在某些时候,您的代码需要User类型的对象,而是获得nil。你在做这样的事吗?

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

哪里,“Mary”是一个不存在的用户登录?发布您调用role_symbols方法的任何地方,我们可以提供更多帮助。

编辑:查看方法#roles_for here,而不深入研究该插件,我会说#current_user在代码执行的这一点上没有设置。

答案 2 :(得分:1)

尚未在表上设置角色属性。您必须使用救援代码,并检查发生这种情况的原因。

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