Ruby-on-rails Rails:未定义的方法“to_sym”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2267163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: Undefined method 'to_sym'
提问by Eric K
I'm getting the following error in my deployed Rails 2.3.5 application:
我在部署的 Rails 2.3.5 应用程序中收到以下错误:
NoMethodError (undefined method `to_sym' for nil:NilClass):
NoMethodError(nil:NilClass 的未定义方法 `to_sym'):
My local testing install of the application, which uses Sqlite, doesn't get the error, but my deployed app running Mysql does. The only other difference between the two is I'm running Ruby 1.8.7 on my local machine and 1.8.6 on my deployment server.
我使用 Sqlite 的应用程序的本地测试安装没有出现错误,但我部署的运行 Mysql 的应用程序会出现错误。两者之间唯一的其他区别是我在本地机器上运行 Ruby 1.8.7,在部署服务器上运行 1.8.6。
I've included the code from User.rband the error log below. I set this up following the Declarative Authorization and Embedded Authorization Railscasts.
我已经包含了User.rb下面的代码和错误日志。我按照声明性授权和嵌入式授权 Railscasts 进行设置。
EDIT: Here's the code for the application_controller, where I set current_userusing before_filter:
编辑:这是 的代码application_controller,我在其中设置current_user使用before_filter:
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:
用户.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
The error log:
错误日志:
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)
回答by JRL
def role_symbols
[role.to_sym]
end
-> roleis Nil. Do you define rolesomewhere?
->role是Nil。你在role某个地方定义了吗?
回答by Ben
At some point, your code is expecting an object of type User, but is getting nilinstead. Are you doing anything like this?
在某些时候,您的代码需要一个类型为 的对象User,但正在获取nil。你在做这样的事情吗?
@user = User.find_by_login("Mary")
@user.role_symbols
Where, "Mary" is a nonexistent user login? Post any places where you are calling the role_symbols method and we can help more.
哪里,“玛丽”是一个不存在的用户登录?发布您调用 role_symbols 方法的任何地方,我们可以提供更多帮助。
Edit:Looking at the method #roles_for here, without digging too deeply into that plugin, I'd say that #current_user is not set at this point of your code execution.
编辑:在这里查看#roles_for 方法,没有深入研究该插件,我会说 #current_user 在您的代码执行点没有设置。
回答by nanda
Role attribute hasn't been set on the table. You have to use a rescue code, and inspect why this happens.
尚未在表上设置角色属性。您必须使用救援代码,并检查发生这种情况的原因。
Test if 'role' value is setted before call role.to_sym.
在调用 role.to_sym 之前测试是否设置了 'role' 值。

