Ruby on Rails Else If Question

I have an application in which there is a div_name user_name in the layout that will display different things depending on whether they are logged in or not, are the administrator, etc. My code now looks like this:

<% 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 already have the current_user helper, and everything works fine when the code was simple:

 <% 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 logged in as an administrator, it works, and I get the text displayed with the correct links. When I am not an administrator / logout, I get the `w90>` role 'method 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_user = current_user_session && current_user_session.record end 

Any ideas what I can do to show the result I want? "If they are a user with a role attribute equal to the administrator, they receive text and registered links, if they are just a user, they receive registered links, and if they are not logged in, they receive registration and login links.

Thanks!

+7
ruby ruby-on-rails
source share
3 answers
 <% 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 %> 
+11
source share

Hide role checking in the current user loop, which has the side effect of simplifying the conditional expression.

 <% 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 %> 
+5
source share
 <% if current_user and current_user.role == "admin" %> 

This should prevent an error when the user is not logged in, but you can rebuild the entire block to remove redundant tests with current_user equal to zero.

+3
source share

All Articles