Ruby-on-rails 如何在不使用身份验证的情况下在 rails 中使用设计获取当前用户!在控制器上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4235686/
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
How to get current user using devise in rails without using authenticate! on the controller
提问by Paul Johnson
I'm using devise with rails 3 and i'm trying to create a page which can be viewed by everyone (even those not signed up) but has additional functionality for people who are registered.
我正在使用带有 rails 3 的设计,并且我正在尝试创建一个可供所有人(即使是未注册的人)查看的页面,但对已注册的人具有附加功能。
The problem is that when I call current_user there is no user because I haven't used the authenticate! filter on my controller, because I want unregistered users to be able to view it.
问题是当我调用 current_user 时没有用户,因为我没有使用身份验证!在我的控制器上过滤,因为我希望未注册的用户能够查看它。
How do I sign in a user if they are in the session otherwise leaving it without a user?
如果用户在会话中,我如何登录用户,否则在没有用户的情况下离开它?
回答by Chandra Patni
You can use user_signed_in?to add additional functionality in views.
您可以使用user_signed_in?在视图中添加其他功能。
<% if user_signed_in? %>
... for logged in users only and current_user is not nil here....
<% else %>
... for anonymous users only and current_user is nil here....
<% end %>
回答by Travis Pessetto
The only way I can think to do this is to call it everytime you want to call current user, which probably isn't the best solution... so it would be something like
我能想到的唯一方法是每次你想调用当前用户时调用它,这可能不是最好的解决方案......所以它会像
if user_signed_in?
#code for current_user
else
#code for no current_user object
end
回答by codesponge
Just a quick note. I wanted to find out if an admin user was logged in, and if so provide some helpful links & info on the public view page, so the admin could quickly jump to the admin section to edit the resource.
只是一个快速的笔记。我想知道管理员用户是否已登录,如果是,请在公共视图页面上提供一些有用的链接和信息,以便管理员可以快速跳转到管理部分来编辑资源。
Howeverif you generate your user as AdminUser(following the getting started instructions for ActiveAdmin) then the methods user_signed_in?and current_userare admin_user_signed_in?and current_admin_user.
但是,如果您将用户生成为 AdminUser(按照 ActiveAdmin 的入门说明进行操作),那么user_signed_in方法是什么?和current_user是admin_user_signed_in?和current_admin_user。
So for example in a public view to show a post (views/posts/show.html.erb) I can use (simplified for clarity)
因此,例如在公共视图中显示帖子(views/posts/show.html.erb)我可以使用(为清晰起见而简化)
<div id='show_post_<%= @post.id %>'>
<h2><%= @post.title %></h2>
<div class='post_author'>by: <%= @post.author%></div>
<% if admin_user_signed_in? %>
<div class='admin_links'>Put links to admin pages for:
<%= current_admin_user.email %>
</div>
<% end %>
<div class='post_body'>
<%= raw @post.content %>
</div>
</div>
I expect that the method names will be generated based on whatever you used when you set up your usersin ActiveAdmin or Devise (if you named your usermodel Vip then the method would be vip_signed_in?).
我希望方法名称将根据您在 ActiveAdmin 或 Devise 中设置用户时使用的任何内容生成(如果您将用户模型命名为Vip,那么该方法将是vip_signed_in?)。
回答by Katy Levinson
Not to nitpick, but it seems like both work in the same places, it is just that current_userisn't initialized if no user is signed in.
不是吹毛求疵,但似乎两者都在同一个地方工作,只是current_user如果没有用户登录,则不会初始化。
Here's a scrap of a line from a related idea, it's from a before_filterwhich prohibits users from editing items which don't belong to them
这是一个相关想法的片段,它来自before_filter禁止用户编辑不属于他们的项目的 a
user_signed_in? && (@subscription.user_id == current_user.id || current_user.admin)
You have to wrap the whole bit with an &&on user_signed_in?though, or it will crash when no user is signed in.
你必须用&&on包裹整个位user_signed_in?,否则当没有用户登录时它会崩溃。

