简体   繁体   English

Rails 3,仅在用户登录时才呈现部分内容

[英]Rails 3, only rendering partial if a user is logged in

On a Rails 3.1 app, I have a method called user_logged_in?在 Rails 3.1 应用程序上,我有一个名为user_logged_in? from Devise, which I want to use to render a partial selectively.来自 Devise,我想用它来选择性地渲染部分。 Here's the code in layouts/application.html.erb:这是 layouts/application.html.erb 中的代码:

<% if user_logged_in? %>
<%= render :partial => "home/statusbar" %>
<% end %>

It uses a partial from a different controller.它使用来自不同 controller 的部分。 This is the partial:这是部分:

<div id="status_bar">  
  Signed in as <%= link_to current_user.name, edit_user_registration_path %>
</div>

An error occurs at current_user.name (undefined method), because current_user is nil . current_user.name (未定义的方法)发生错误,因为current_usernil I could move the user_logged_in?我可以移动user_logged_in? check inside the partial, but I think it would be marginally more efficient if it were in the view.检查部分内部,但我认为如果它在视图中会稍微更有效率。

Could you direct me to what the best practices are, or how I can overcome the undefined method error?您能否指导我了解最佳实践是什么,或者我如何克服未定义的方法错误? Is it normal that Rails checks the code of the template even though it's in an if block that fails? Rails 检查模板的代码是否正常,即使它位于失败的if块中?

It looks like user_logged_in?它看起来像user_logged_in? is where something is going wrong.Does it work if you change to this?是哪里出了问题。如果你改变它,它会起作用吗?

<% if current_user.present? %>
  <%= render :partial => "home/statusbar" %>
<% end %>

I'm familiar with Devise, but not the user_logged_in?我熟悉 Devise,但不熟悉user_logged_in? method - did you define it yourself?方法 - 你自己定义的吗? The normal helper used in views is user_signed_in?视图中使用的普通助手是user_signed_in? . . Also, try something like the following:另外,请尝试以下操作:

<% puts user_logged_in?.inspect %>
<% puts user_signed_in?.inspect %>

Above the if statement in your view, and check your logs to make sure your call is returning false correctly ( user_signed_in? should also be returning false ).在您视图中的 if 语句上方,并检查您的日志以确保您的调用正确返回falseuser_signed_in?也应该返回false )。 You should not be getting a nil error in that partial unless it is actually being rendered (meaning the code entered the if statement).您不应该在该部分中收到 nil 错误,除非它实际上正在呈现(意味着代码输入了 if 语句)。

I found the solution, it appears that我找到了解决方案,看来

user_logged_in?

Is not used anymore, and instead you need to use不再使用,而是您需要使用

user_signed_in?

So with the code you gave this should work:因此,使用您提供的代码应该可以工作:

<% if user_signed_in? %>
  <%= render :partial => "home/statusbar" %>
<% end %>

This code rendered a partial for me.这段代码为我呈现了一部分。

That code is being run, ruby is duck typed and dynamic, all checks are at runtime.该代码正在运行,ruby 是鸭子类型和动态的,所有检查都在运行时进行。 So you need to check your conditional.所以你需要检查你的条件。 Try尝试

<% if false %>
  <%= render :partial => "home/statusbar" %>
<% end %>

and see if it stll fails.看看它是否仍然失败。 A bettter solution would be更好的解决方案是

<% if current_user %>
  <%= render :partial => "home/statusbar" %>
<% end %>

I am unfamiliar with devise but this should work Since current user wil be nil if nobody is logged in我不熟悉 devise 但这应该可以工作因为如果没有人登录,当前用户将为零

There is an even shorter way to render your partial with a conditional有一个更短的方法来渲染你的部分有条件

<%= render(partial: "home/statusbar") if current_user.present? %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM