Rails 设计:user_signed_in?不工作
我的观点是:
<% 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 %>
但是当以用户身份登录时: 我仍然看到链接:
<%= link_to "Sign in", new_user_session_path %><br />
<%= link_to "Opret", new_user_session_path %><br />
为什么助手不起作用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您在控制器中的操作之前使用过设备吗?
Did you use devise's before action in your controller?
不确定 user_signed_in 后面是什么?方法,要么您的登录无法正常工作,要么您的方法已损坏。
也许尝试一下,如果这不起作用,我会看看实际登录出了什么问题。
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.
确保您使用的是正确的型号。例如,如果您的模型是会员,那么您应该使用
member_sign_in
而不是user_sign_in
。Make sure that you are using the right model. For example if your model is member then you should use
member_sign_in
instead ofuser_sign_in
.这可能是一个可能的解释。
您可能有一个预先存在的
current_user
方法,该方法会阻止在user_signed_in?
中调用的 Devise 的current_user
返回预期值。This could be a possible explanation.
You might have a pre-existing
current_user
method which stops Devise'scurrent_user
called withinuser_signed_in?
from returning expected values.如果登录正常工作,可能是因为正在进行某种缓存?
If login is working properly, it could be because of some kind of caching going on?
我遇到了完全相同的问题并通过这样做解决了它
再次
$bundle execrails g devise user
。我的问题是devise_for users
在路由文件中不知何故丢失了。I had encountered exactly the same issue and solved it by doing
$bundle exec rails g devise user
again. My problem wasdevise_for users
was somehow missing in the routing file.如果您在操作中使用它,请确保在前面添加冒号:
而不是
If you are using it within an action, make sure you add the colon before:
Instead of