Ruby on Rails Else If Question
I have an application where in the layout I have a user_name div which will display different things based on whether or not they are logged in, are an admin, etc. Right now my code is the following:
<% if current_user.role == "admin" %>
<p id="admintxt">You are an admin!</p>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% elsif current_user %>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% else %>
<%= link_to "Register", new_user_path %>
<%= link_to "Login", login_path %>
<% end %>
I have a current_user helper already and everything was working fine when the code was just:
<% if current_user %>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% else %>
<%= link_to "Register", new_user_path %>
<%= link_to "Login", login_path %>
<% end %>
Now when I make it an elsif statement, when I am logged in as an admin it works and I get the text displayed with the correct links. When I am not an admin user/logged out, I get a undefined method `role' for nil:NilClass error... Also my current_user stuff is declared in my application controller as follows:
helper_method :current_user
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current开发者_如何学C_user = current_user_session && current_user_session.record
end
Any ideas what I can do to display the result I want? "If they are a user with the role attribute equal to admin they get some text and the logged in links, if they are just a user they get the logged in links, and if they are not logged in they get the register and login links.
Thanks!
<% if current_user %>
<% if current_user.role == "admin" %>
<p id="admintxt">You are an admin!</p>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% else %>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% end %>
<% else %>
<%= link_to "Register", new_user_path %>
<%= link_to "Login", login_path %>
<% end %>
or with Rails >= 2.3
<% if current_user.try(:role) == "admin" %>
<p id="admintxt">You are an admin!</p>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% elsif current_user %>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% else %>
<%= link_to "Register", new_user_path %>
<%= link_to "Login", login_path %>
<% end %>
Hide the role check in the current user loop, which has the side effect of simplifying your conditional.
<% if current_user %>
<%= content_tag(:p, "You are an admin!", :id=>"admintxt") if current_user.role == "admin" %>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% else %>
<%= link_to "Register", new_user_path %>
<%= link_to "Login", login_path %>
<% end %>
<% if current_user and current_user.role == "admin" %>
This should prevent the error when there is no user logged in, but you could restructure the whole block in order to remove the redundant tests against current_user being nil.
精彩评论