Rails devise: user_signed_in? not working

2019-01-12 02:59发布

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?

6条回答
劳资没心,怎么记你
2楼-- · 2019-01-12 03:01

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

before_filter :authenticate_user!
查看更多
在下西门庆
3楼-- · 2019-01-12 03:02

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.

查看更多
SAY GOODBYE
4楼-- · 2019-01-12 03:03

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.

查看更多
你好瞎i
5楼-- · 2019-01-12 03:13

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.

查看更多
该账号已被封号
6楼-- · 2019-01-12 03:20

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-%>
查看更多
Lonely孤独者°
7楼-- · 2019-01-12 03:27

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

查看更多
登录 后发表回答