正如标题所暗示的那样,设计帮助程序方法仅在users_controller.rb中工作。如果我在登录后尝试在任何其他控制器(甚至 applications_controller.rb(中访问它,我会收到此错误。
Routing Error
undefined local variable or method `current_user' for ApplicationController:Class
我只是在 applications_controller.rb 中添加puts current_user
。
这是我的routes.rb
devise_for :users
resources :users
知道为什么会这样吗?谢谢。
current_user 不是类方法,因此它不会直接在 ApplicationController 中使用
它将在所有action
和before_action
中可用
使用以下逻辑检查应用程序控制器中current_user
的值。
class ApplicationController < ActionController::Base
before_action do
puts current_user
end
protect_from_forgery with: :exception
end
如果您仍然遇到任何问题,请告诉我