Ruby-on-rails 设计助手(authenticate_user!、current_user、user_signed_in?)未初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16801561/
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
Devise Helpers (authenticate_user!, current_user, user_signed_in?) not initialized
提问by Btuman
I am setting up devise and need to be able to use devise helpers.
When I add before_filter :authenticate_user!into the application controller I get the following error undefined method 'authenticate_user!' for #<HomeController:*>
我正在设置设计并且需要能够使用设计助手。当我添加 before_filter :authenticate_user!到应用程序控制器时,出现以下错误undefined method 'authenticate_user!' for #<HomeController:*>
It is not present in my home controller directly, but inherited from the application controller, if I use skip_before_filter :authenticate_user!in my home (or any controller) and try to access the page I get the following error.
它不直接存在于我的家庭控制器中,而是从应用程序控制器继承的,如果我skip_before_filter :authenticate_user!在我的家庭(或任何控制器)中使用并尝试访问该页面,我会收到以下错误。
undefined method `user_signed_in?' for #<#<Class *>
It breaks on the line that in the layouts/application.html.erb file that calls it. The same things happens for any of the helpers. It looks as if they are not being loaded at all, however, when I loaded up the rails console, and entered $LOAD_PATH.dupthe output included
它在调用它的 layouts/application.html.erb 文件中的那一行中断。任何一个助手都会发生同样的事情。但是,当我加载 rails 控制台并输入$LOAD_PATH.dup包含的输出时,它们看起来好像根本没有加载
"/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/controllers", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/helpers", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/mailers"
So it seems that they should indeed be loaded by rails.
所以看起来它们确实应该由rails加载。
This is my application controller
这是我的应用程序控制器
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery
include ActionView::Helpers::NumberHelper
before_filter :authenticate_user!end
类 ApplicationController < ActionController::Base
helper :all # 包含所有助手,一直以来
protect_from_forgery
包含ActionView::Helpers::NumberHelper
before_filter :authenticate_user!结尾
I did restart the server after installing devise, and multiple times as I tried different attempts to solve the problem. If any more files or information is needed please let me know. Thank you very much in advance.
安装设备后,我确实重新启动了服务器,并且多次尝试不同的尝试来解决问题。如果需要更多文件或信息,请告诉我。非常感谢您提前。
Edit: My user.rb file was requested to show that I have indeed installed devise
编辑:我的 user.rb 文件被要求显示我确实安装了设计
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:encryptable, :encryptor => :restful_authentication_sha1
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
Edit: The entire error message
编辑:整个错误信息
NoMethodError in HomeController#index
undefined method `authenticate_user!' for #< HomeController:0x000000040c45a0 > '
HomeController 中的 NoMethodError#index
未定义的方法 `authenticate_user!' for #< HomeController:0x000000040c45a0 > '
Here is the full trace
这是完整的跟踪
activesupport (3.2.13) lib/active_support/callbacks.rb:418:in `run_2094683035699451847__process_action_2229546931289158113_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in >`_run_process_action_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `run_177144612565476380_call_4534346825225857812__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'
passenger (4.0.2) lib/phusion_passenger/rack/thread_handler_extension.rb:77:in `process_request'
passenger (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:135:in `accept_and_process_next_request'
passenger (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:106:in `main_loop'
passenger (4.0.2) lib/phusion_passenger/request_handler.rb:449:in `block (4 levels) in start_threads'
passenger (4.0.2) lib/phusion_passenger/utils/robust_interruption.rb:108:in `disable_interruptions'
passenger (4.0.2) lib/phusion_passenger/request_handler.rb:444:in `block (3 levels) in start_threads'
的ActiveSupport(3.2.13)LIB / active_support / callbacks.rb:418:在`运行_2094683035699451847__process_action_ 2229546931289158113个_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in >`_run_process_action_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
旅程 (1.0.4) lib/journey/router.rb:68:in `block in call'
旅程 (1.0.4) lib/journey/router.rb:56:in `each'
旅程 (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
Warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
典狱长 (1.2.1) lib/warden/manager.rb:34:in `catch'
典狱长 (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `运行_177144612565476380_调用_4534346825225857812__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
机架 (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'
乘客 (4.0.2) lib/phusion_passenger/rack/thread_handler_extension.rb:77:in `process_request'
乘客 (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:135:in `accept_and_process_next_request'
乘客 (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:106:in `main_loop'
乘客 (4.0.2) lib/phusion_passenger/request_handler.rb:449:in ‘block (4 levels) in start_threads’
乘客 (4.0.2) lib/phusion_passenger/utils/robust_interruption.rb:108:in `disable_interruptions'
乘客 (4.0.2) lib/phusion_passenger/request_handler.rb:444:in ‘block (3 levels) in start_threads’
Edit: This is the the text from running Devise.helpers in the console.
编辑:这是在控制台中运行 Devise.helpers 的文本。
$ rails console
Loading development environment (Rails 3.2.13)
1.9.3-p429 :001 > Devise.helpers
=> #< Set: {Devise::Controllers::Helpers} >
$ 导轨控制台
加载开发环境(Rails 3.2.13)
1.9.3-p429 :001 > Devise.helpers
=> #< 设置:{设计::控制器::助手} >
Edit: This my routes rb file. :Application.routes.draw do
编辑:这是我的路由 rb 文件。:Application.routes.draw 做
devise_for :users
resource :sessions, :only => [:new, :create, :destroy]
devise_scope :user do
match 'signup' => 'users#new', :as => :signup
match 'register' => 'users#create', :as => :register
match '/login' => 'sessions#new', :as => :login
match 'logout' => 'sessions#destroy', :as => :logout
end
match '/activate/:activation_code' => 'users#activate', :as => :activate, :activation_code => nil
match '/users/:id', :to => 'users#show', :as => :user
resources :users do
member do
put :suspend
put :unsuspend
delete :purge
end
end
resource :sessions
other resources
match '' => 'home#index', :as => :home
match ':controller(/:action(/:id))'
root to: 'home#index'
end
回答by paulguy
Be sure to add devise_for :userin your routes.rb
请务必添加devise_for :user在您的routes.rb
See more here: https://stackoverflow.com/a/11180576
在此处查看更多信息:https: //stackoverflow.com/a/11180576
回答by kunysch
Just restart your rails server.
只需重新启动 Rails 服务器。
"Note that you should re-start your app here if you've already started it. Otherwise you'll run into strange errors like users being unable to login and the route helpers being undefined." [Getting Started Instructions]
“请注意,如果您已经启动了应用程序,则应在此处重新启动它。否则您会遇到奇怪的错误,例如用户无法登录和路由助手未定义。” [入门说明]
[edit] More detailed answer:
[编辑] 更详细的答案:
Please verify two additional things: Your HomeController should inherit from Devise::Controllers::Helpers. And the devise directive in route.rb (that generates the missing method in Helpers) should have extended Devise.@@mappings. The following code can check this:
请验证另外两件事:您的 HomeController 应该从 Devise::Controllers::Helpers 继承。而 route.rb 中的 devise 指令(在 Helpers 中生成缺少的方法)应该扩展了 Devise.@@mappings。以下代码可以检查这一点:
class ApplicationController ...
...
before_filter do
fail "bad ancestor" unless self.kind_of?(Devise::Controllers::Helpers)
fail "no mapping" unless Devise.class_variable_get(:@@mappings).[:user]
authenticate_user!
end
end
To introduce a similar problem in a working rails application the config/initializers/devise.rb and the config/routes.rb devise entries have to be disabled and rails has to be restarted. Reenabling the code will not fix the problem unless rails is restarted.
要在工作中的 rails 应用程序中引入类似的问题,必须禁用 config/initializers/devise.rb 和 config/routes.rb 设计条目,并且必须重新启动 rails。除非重新启动 rails,否则重新启用代码不会解决问题。
回答by Christopher Oezbek
There are many things which can go wrong. Some things to check in order:
有很多事情可能会出错。需要按顺序检查的一些事项:
- Not being able to call
before_filter :authenticate_user!indicates that devise is not correctly set up. - Add
devise_for :usersin routes.rb (its is users, you got this right), next simplify those scoped rules for testing. - You need to have the
before_filter :authenticate_user!working to get the helpers in the controller or include the helpers manually - If your base controller does not inherit from ActionController then you need to add the helper methods manually: Put this in your base controller
helper_method "current_user", "user_signed_in?", "user_session" - One thing to check: Does
self.controller.current_userwork in your erb? This would indicate the helper_methods not being included.
- 无法调用
before_filter :authenticate_user!表示设备设置不正确。 - 添加
devise_for :usersroutes.rb (它是 user s,你做对了),接下来简化那些用于测试的范围规则。 - 您需要
before_filter :authenticate_user!努力在控制器中获取助手或手动包含助手 - 如果您的基本控制器不是从 ActionController 继承的,那么您需要手动添加辅助方法:将其放在您的基本控制器中
helper_method "current_user", "user_signed_in?", "user_session" - 要检查的一件事:
self.controller.current_user在您的 erb 中有效吗?这将表明不包含 helper_methods。
回答by AmitF
I added this line to my controller which did the trick for me:
我将此行添加到我的控制器中,这对我有用:
include Devise::Controllers::Helpers
回答by Roshan
I had the same problem (Rails 5.1.2, Devise 4.3.0) and it happened to be that I was using :-
我遇到了同样的问题(Rails 5.1.2,Devise 4.3.0),碰巧我正在使用:-
devise_for :user do
...
end
instead of
代替
devise_for :users do
...
end
I fixed it by changing the namespace.
我通过更改命名空间来修复它。
user => users
用户 => 用户
回答by Niek
I had the same problem following Devise's getting started guide.
按照 Devise 的入门指南,我遇到了同样的问题。
Turns out I had mistakenly run rails generate model Userrather than rails generate devise User, which just created a plain ActiveRecord model that happened to have the name 'User'.
结果我错误地运行了rails generate model User而不是rails generate devise User,它只是创建了一个普通的 ActiveRecord 模型,它碰巧具有名称“用户”。

