17

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?

John
  • 171
  • 1
  • 1
  • 3

7 Answers7

22

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

before_action :authenticate_user!
Ri1a
  • 737
  • 9
  • 26
Brian
  • 4,931
  • 3
  • 32
  • 55
  • what controller should this go in? I used devise and I have 'controllers/users/omniauth_callbacks_controller.rb` and also `application_controller.rb` – Connor Leech Nov 18 '13 at 10:11
  • 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. – Anil Muppalla Dec 02 '13 at 15:47
8

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-%>
Scott
  • 1,105
  • 1
  • 7
  • 17
5

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.

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
ramez emad
  • 53
  • 1
  • 4
3

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.

Community
  • 1
  • 1
1

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

rohit.arondekar
  • 416
  • 3
  • 10
0

I had encountered exactly the same issue and solved it by doing $bundle exec rails g devise user again. My problem was devise_for users was somehow missing in the routing file.

cat
  • 1
  • 3
  • devise_for was missing and hence helpers were not recognized in the application views etc. – cat Mar 14 '16 at 03:26
0

If you are using it within an action, make sure you add the colon before:

before_action :set_global_variables, if: :user_signed_in?

Instead of

before_action :set_global_variables, if: user_signed_in?
drjorgepolanco
  • 7,479
  • 5
  • 46
  • 47