Ruby-on-rails Rails 设计:user_signed_in?不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6003883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:08:21  来源:igfitidea点击:

Rails devise: user_signed_in? not working

ruby-on-railsrubyruby-on-rails-3devise

提问by John

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?

为什么助手不工作?

回答by Brian

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

您是否在控制器中使用了设计之前的过滤器?

before_filter :authenticate_user!

回答by Scott

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

不确定 user_signed_in 背后是什么?方法,要么您的登录无法正常工作,要么您的方法已损坏。

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-%>

回答by ramez emad

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

确保您使用的是正确的模型。例如,如果您的模型是成员,那么您应该使用member_sign_in而不是user_sign_in.

回答by Mayank Singh

Thiscould be a possible explanation.

可能是一个可能的解释。

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

您可能有一个预先存在的current_user方法,可以阻止 Devise 的current_user内部调用user_signed_in?返回预期值。

回答by rohit.arondekar

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

如果登录工作正常,可能是因为正在进行某种缓存?

回答by cat

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

我遇到了完全相同的问题,并通过$bundle exec rails g devise user再次执行解决了它 。我的问题是devise_for users路由文件中不知何故丢失了。