On a Rails 3.1 app, I have a method called user_logged_in? from Devise, which I want to use to render a partial selectively. Here’s the code in layouts/application.html.erb:
<% if user_logged_in? %>
<%= render :partial => "home/statusbar" %>
<% end %>
It uses a partial from a different 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. I could move the 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?
It looks like
user_logged_in?is where something is going wrong.Does it work if you change to this?