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 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.
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 ).
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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With