Rails 设计:user_signed_in?不工作

发布于 2024-11-07 17:56:52 字数 471 浏览 2 评论 0原文

我的观点是:

<% 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

多像笑话 2024-11-14 17:56:52

您在控制器中的操作之前使用过设备吗?

before_action :authenticate_user!

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

before_action :authenticate_user!
挽手叙旧 2024-11-14 17:56:52

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

也许尝试一下,如果这不起作用,我会看看实际登录出了什么问题。

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

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-%>
若言繁花未落 2024-11-14 17:56:52

确保您使用的是正确的型号。例如,如果您的模型是会员,那么您应该使用 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 of user_sign_in.

痕至 2024-11-14 17:56:52

可能是一个可能的解释。

您可能有一个预先存在的 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's current_user called within user_signed_in? from returning expected values.

十年九夏 2024-11-14 17:56:52

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

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

千と千尋 2024-11-14 17:56:52

我遇到了完全相同的问题并通过这样做解决了它
再次$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 was devise_for users was somehow missing in the routing file.

睡美人的小仙女 2024-11-14 17:56:52

如果您在操作中使用它,请确保在前面添加冒号:

before_action :set_global_variables, if: :user_signed_in?

而不是

before_action :set_global_variables, if: user_signed_in?

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?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文