Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 676
Paul Johnson Avatar asked Nov 20 '10 23:11

Paul Johnson


People also ask

How do I get a current user in devise?

1 Answer. Show activity on this post. If user is not logged in, i.e., current_user=nil then user would be redirected to login page with a flash message.

What is devise gem in rails?

Devise is the cornerstone gem for Ruby on Rails authentication. With Devise, creating a User that can log in and out of your application is so simple because Devise takes care of all the controllers necessary for user creation ( users_controller ) and for user sessions ( users_sessions_controller ).


1 Answers

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 %>
like image 105
Chandra Patni Avatar answered Oct 15 '22 20:10

Chandra Patni