Ruby-on-rails 在 rails 控制器中访问设备 current_user 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19147729/
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
Access devise current_user variable in rails controller
提问by Dima Knivets
I've got Rails (4.0) running Devise gem (3.1.0) using model User. I have controller named CollectionsControllerand I want to get current logged in user object with Devise's accessor method current_userin this controller.
我有 Rails (4.0) 使用 model 运行 Devise gem (3.1.0) User。我有一个命名的控制器CollectionsController,我想current_user在这个控制器中使用 Devise 的访问器方法获取当前登录的用户对象。
And after that it returns undefined local variable or method 'current_user' for CollectionsController:Class. The most interesting thing is that when I'm trying to do the same in another controller, for example PagesController— everything works perfectly!
UPD:sharing the "code" of my controller :)
之后它返回undefined local variable or method 'current_user' for CollectionsController:Class。最有趣的是,例如,当我尝试在另一个控制器中执行相同操作时PagesController- 一切正常!
UPD:共享我的控制器的“代码”:)
class CollectionsController < ActionController::Base
def index
@user = current_user
end
end
the source of current_usermethod is defined by Devise, not me. So I don't think that is the problem.
current_user方法的来源是由 Devise 定义的,而不是我。所以我不认为这是问题所在。
回答by zeantsoi
current_useris a convenience method made available by Devise to ApplicationController. Your controller should be inheriting from it:
current_user是 Devise 提供的一种方便的方法ApplicationController。您的控制器应该继承它:
class CollectionsController < ApplicationController
It seems you may be conflating ActiveRecord::Base(subclassed by models) with ActionController(subclassed by controllers). According to Rails docs:
看来您可能将ActiveRecord::Base(由模型子类化)与ActionController(由控制器子类化)混为一谈。根据Rails 文档:
By default, only the ApplicationController in a Rails application inherits from ActionController::Base. All other controllers in turn inherit from ApplicationController.
默认情况下,只有 Rails 应用程序中的 ApplicationController 继承自 ActionController::Base。所有其他控制器依次继承自 ApplicationController。
回答by Yuki Matsukura
Add before_action :authenticate_user!
to your controller.
添加before_action :authenticate_user!
到您的控制器。
see: https://github.com/plataformatec/devise#controller-filters-and-helpers
见:https: //github.com/plataformatec/devise#controller-filters-and-helpers
回答by user3468739
I was having the same problem. Some controllers could access current_userand others could not. In one case, it was an erbfile that was accessing current_userbut only after asking user_signed_in?I added that check in my controller code and voila! current_userwas available.
我遇到了同样的问题。一些控制器可以访问,current_user而另一些则不能。在一种情况下,它是一个erb正在访问的文件,current_user但只有在询问user_signed_in?我在控制器代码中添加了该检查之后,瞧! current_user可用。
回答by Shekhar Patil
class CollectionsController < ApplicationController
include Devise::Controllers::Helpers
def index
@user = current_user
end
end
Add (include Devise::Controllers::Helpers) in your controller as shown above.
在您的控制器中添加(包括 Devise::Controllers::Helpers),如上所示。

