How to get current user using devise in rails without using authenticate! on the controller

Paul Johnson picture Paul Johnson · Nov 21, 2010 · Viewed 26.1k times · Source

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?

Answer

Chandra Patni picture Chandra Patni · Nov 21, 2010

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