Ruby-on-rails Rails Devise:获取当前登录用户的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4149326/
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 Devise: get object of the currently logged in user?
提问by GSto
I've recently installed Devise on a rails application, and I am wondering if it is possible to get an instance of the currently logged in user in either one of the other models or controllers, and if so, how do I do that?
我最近在 Rails 应用程序上安装了 Devise,我想知道是否可以在其他模型或控制器之一中获取当前登录用户的实例,如果是这样,我该怎么做?
回答by Scott
Devise creates convenience methods on the fly that represent your currently logged user.
Devise 即时创建方便的方法来代表您当前登录的用户。
Howeveryou should note that the generated method name includes the class name of your user model. e.g. if your Devise model is called 'User' then the currently logged in user can be accessed with 'current_user', and if your Devise class is 'Admin' then the logged in admin user can be accessed with 'current_admin'.
但是您应该注意,生成的方法名称包括您的用户模型的类名称。例如,如果您的 Devise 模型称为“ User”,则可以使用“ current_user”访问当前登录的用户,如果您的 Devise 类是“ Admin”,则可以使用“ current_admin”访问登录的管理员用户。
There are a number of other methods created with similar conventions, for example 'user_signed_in?' or again 'admin_signed_in?', which are really nice.
有许多其他方法使用类似的约定创建,例如“ user_signed_in?”或再次“ admin_signed_in?”,它们非常好。
These methods are available in controllers and views so you might have the following in a view:
这些方法在控制器和视图中可用,因此您可能在视图中拥有以下内容:
<% if user_signed_in? %>
<div>Signed in as... <%= current_user.email %></div>
<% end %>
Finally, if you are using two or more Devise models in your app (e.g. User and Admin), you can use the 'anybody_signed_in?' convenience method to check if either of those types of user are signed in:
最后,如果您在应用程序中使用两个或多个 Devise 模型(例如 User 和 Admin),您可以使用“ anybody_signed_in?”方便的方法来检查这两种类型的用户是否已登录:
<% if anybody_signed_in? %>
<h2>Special offers</h2>
<p>All registered users will receive free monkeys!</p>
<% end %>
Update:
更新:
Since Devise version 1.2.0, 'anybody_signed_in?' has been deprecated and replaced by 'signed_in?'
从 Devise 版本 1.2.0 开始,' anybody_signed_in?' 已被弃用并由 ' signed_in?'取代
回答by John
The Devise helper methods are only available at the controller and view layers. They are not available at the model layer (see Controller filters and helpers section of README).
Devise 辅助方法仅在控制器和视图层可用。它们在模型层不可用(请参阅自述文件的控制器过滤器和帮助器部分)。
Is it possible to get the currently logged in user from within a model?.
Is it possible to get the currently logged in user from within a model?.
It is not possible via the default helper methods that Devise creates for you. However, there are many alternative methods you can use to provide the current_userto a model. The simplest way has already been suggested by Alex Bartlow, and that is to simply pass the current_uservia a method to your model.
通过 Devise 为您创建的默认辅助方法是不可能的。但是,您可以使用许多替代方法将 提供current_user给模型。Alex Bartlow 已经建议了最简单的方法,那就是简单地将current_uservia 方法传递给您的模型。
Is it possible to get the currently logged in user from within a controllers?
Is it possible to get the currently logged in user from within a controllers?
Yes it is possible. Use current_<modelname>, where <modelname>is the name of the model that has Devise authentication capabilities (i.e., via rails g devise <modelname>). If, for example, your model is User, then you would use current_user. If your model is Elmo, then you would use current_elmo.
对的,这是可能的。使用current_<modelname>,其中<modelname>是具有设计身份验证功能的模型的名称(即通过rails g devise <modelname>)。例如,如果您的模型是User,那么您将使用current_user. 如果您的模型是Elmo,那么您将使用current_elmo.
回答by MaximuSSmile
simple method is:
简单的方法是:
if @suit.user == current_user
example:
例子:
= link_to "Back", root_path, class: "btn btn-default"
-if @suit.user == current_user
= link_to "Edit", edit_suit_path, class: "btn btn-default"
= link_to "Delete", suit_path, method: :delete, data: {confirm: "Are you sure?" }, class: "btn btn-default"
回答by Alex Bartlow
Pass it in as a parameter to the method call :).
将它作为参数传递给方法调用:)。
One idea is to use Thread.current[:current_user] = @current_useras a before_filter - but if you're using a deployment stack like Thin + EM_Mysql2 + Rack::FiberPool, you'll need to set that to Fiber.current[:current_user].
一个想法是Thread.current[:current_user] = @current_user用作 before_filter - 但如果您使用像 Thin + EM_Mysql2 + Rack::FiberPool 这样的部署堆栈,则需要将其设置为Fiber.current[:current_user].
Those solutions are really just covering up for a lack of good design logic.
这些解决方案实际上只是掩盖了缺乏良好的设计逻辑。

