如何在不使用身份验证的情况下在rails中使用设计获得当前用户!在控制器

时间:2021-10-20 15:27:17

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?

如果用户在会话中,我如何在没有用户的情况下登录?

4 个解决方案

#1


14  

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 %>

#2


1  

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

#3


1  

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.

仅仅是一个简短的说明。我想知道一个管理员用户是否登录了,如果登录了,在公共视图页面上提供一些有用的链接和信息,这样管理员可以快速跳转到管理部分来编辑资源。

However if you generate your user as AdminUser(following the getting started instructions for ActiveAdmin) then the methods user_signed_in? and current_user are 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)

例如,在公共视图中显示一个post (view /post /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 users in ActiveAdmin or Devise (if you named your user model Vip then the method would be vip_signed_in?).

我预计方法名将基于您在ActiveAdmin或designs中设置用户时使用的任何方法生成(如果您将用户模型命名为Vip,那么方法将是vip_signed_in?)

#4


0  

Not to nitpick, but it seems like both work in the same places, it is just that current_user isn'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_filter which prohibits users from editing items which don't belong to them

这里有一个与此相关的想法的片段,它来自一个before_filter,它禁止用户编辑不属于他们的条目。

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来包装整个位元?但是,当没有用户登录时,它会崩溃。

#1


14  

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 %>

#2


1  

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

#3


1  

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.

仅仅是一个简短的说明。我想知道一个管理员用户是否登录了,如果登录了,在公共视图页面上提供一些有用的链接和信息,这样管理员可以快速跳转到管理部分来编辑资源。

However if you generate your user as AdminUser(following the getting started instructions for ActiveAdmin) then the methods user_signed_in? and current_user are 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)

例如,在公共视图中显示一个post (view /post /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 users in ActiveAdmin or Devise (if you named your user model Vip then the method would be vip_signed_in?).

我预计方法名将基于您在ActiveAdmin或designs中设置用户时使用的任何方法生成(如果您将用户模型命名为Vip,那么方法将是vip_signed_in?)

#4


0  

Not to nitpick, but it seems like both work in the same places, it is just that current_user isn'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_filter which prohibits users from editing items which don't belong to them

这里有一个与此相关的想法的片段,它来自一个before_filter,它禁止用户编辑不属于他们的条目。

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来包装整个位元?但是,当没有用户登录时,它会崩溃。