Rails devise: user_signed_in? not working

37,404

Solution 1

Did you use devise's before action in your controller?

before_action :authenticate_user!

Solution 2

Not sure what is behind the user_signed_in? method, either your login is not working correctly or your method is broken.

Maybe try this, if that doesn't work, I would take a look at whats going wrong with the actual login.

<%unless current_user.blank? -%>
signed in
<%else -%>
not signed in
<%end-%>

Solution 3

Make sure that you are using the right model. For example if your model is member then you should use member_sign_in instead of user_sign_in.

Solution 4

This could be a possible explanation.

You might have a pre-existing current_user method which stops Devise's current_user called within user_signed_in? from returning expected values.

Solution 5

If login is working properly, it could be because of some kind of caching going on?

Share:
37,404
Admin
Author by

Admin

Updated on September 16, 2021

Comments

  • Admin
    Admin over 2 years

    I have this in my view:

    <% if user_signed_in? %>
    <%= current_user.email %>
    <% else %>
    <%= link_to "Sign in", new_user_session_path %><br />
    <%= link_to "Opret", new_user_session_path %><br />
    <% end %>
    

    But when have signed in as a user: I still get the links in view:

    <%= link_to "Sign in", new_user_session_path %><br />
    <%= link_to "Opret", new_user_session_path %><br />
    

    Why is the helper not working?

  • Connor Leech
    Connor Leech over 10 years
    what controller should this go in? I used devise and I have 'controllers/users/omniauth_callbacks_controller.rb` and also application_controller.rb
  • Anil Muppalla
    Anil Muppalla over 10 years
    It should go in the controller for which the view you are trying to manipulate, you can make do with the if loops in your question.
  • cat
    cat about 8 years
    devise_for was missing and hence helpers were not recognized in the application views etc.