Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails Else If Question
    primarykey
    data
    text
    <p>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:</p> <pre><code> &lt;% if current_user.role == "admin" %&gt; &lt;p id="admintxt"&gt;You are an admin!&lt;/p&gt; &lt;%= link_to "Edit Profile", edit_user_path(:current) %&gt; &lt;%= link_to "Logout", logout_path %&gt; &lt;% elsif current_user %&gt; &lt;%= link_to "Edit Profile", edit_user_path(:current) %&gt; &lt;%= link_to "Logout", logout_path %&gt; &lt;% else %&gt; &lt;%= link_to "Register", new_user_path %&gt; &lt;%= link_to "Login", login_path %&gt; &lt;% end %&gt; </code></pre> <p>I have a current_user helper already and everything was working fine when the code was just:</p> <pre><code>&lt;% if current_user %&gt; &lt;%= link_to "Edit Profile", edit_user_path(:current) %&gt; &lt;%= link_to "Logout", logout_path %&gt; &lt;% else %&gt; &lt;%= link_to "Register", new_user_path %&gt; &lt;%= link_to "Login", login_path %&gt; &lt;% end %&gt; </code></pre> <p>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:</p> <pre><code>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 &amp;&amp; current_user_session.record end </code></pre> <p>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.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload