18

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.

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.

How do I sign in a user if they are in the session otherwise leaving it without a user?

1
  • 5
    If this question is still active, Devise now always loads current_user for you regardless of using the authenticate_user! before filter or not.
    – zykadelic
    Oct 26, 2012 at 16:04

4 Answers 4

14

You can use user_signed_in? to add additional functionality in views.

<% 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 %>
1
  • 2
    Yeah except, user_signed_in? can be true and current_user can be nil. What I want to do is make it load the current_user object if someone is logged in but not if they are not, and not require the user to be authenticated. Nov 21, 2010 at 5:56
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
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.

So for example in a public view to show a post (views/posts/show.html.erb) I can use (simplified for clarity)

<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?).

0
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.

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

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.