Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if, elsif, else statements html erb beginner

I'm having problems with an if, elsif, else statment in html.erb. I've seen a lot of questions around the if/else statements in erb but none that include elsif so I thought I'd ask for help.

Here is my html.erb:

<% if logged_in? %>

          <ul class = "nav navbar-nav pull-right">
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
              Account <b class="caret"></b>
            </a>

          <ul class="dropdown-menu pull-right">
                  <li><%= link_to "Profile", current_user %></li>
                  <li><%= link_to "Settings", edit_user_path(current_user) %></li>
                  <li class="divider"></li>
                  <li>
                    <%= link_to "Log out", logout_path, method: "delete" %>
            </li>
          </ul>
          </li>
        </ul>



     <% elsif has_booth?(current_user.id) %>

      <ul>

        <li>TEST</li>

      </ul>



<% else %>
        <ul class="nav navbar-nav pull-right">
          <li><%= link_to "Sign Up", signup_path %></li>
          <li><%= link_to "Log in", login_path %></li>
        </ul>
      <% end %>

Here is my has_booths method:

module BoothsHelper

def has_booth?(user_id)
  Booth.exists?(user_id: user_id)
end 

end

I would like the header nav to have three different types of content for different users. The logged in user, the logged in user that has created a booth, and the logged out user. So far, I can only seem to make 2 out of the three work. I tried changing

<% elsif has_booth?(current_user.id) %>

to

<% elsif logged_in? && has_booth?(current_user.id) %>

and that did not work either. Am I writing my statement correctly? Any thoughts appreciated. Thanks.

like image 400
Kelly Avatar asked Nov 10 '14 19:11

Kelly


People also ask

What is an ERB template?

An ERB template looks like a plain-text document interspersed with tags containing Ruby code. When evaluated, this tagged code can modify text in the template. Puppet passes data to templates via special objects and variables, which you can use in the tagged Ruby code to control the templates' output.

Does Ruby have else if?

Notice Ruby uses elsif, not else if nor elif. Executes code if the conditional is true. If the conditional is not true, code specified in the else clause is executed.


2 Answers

The problem is that your first condition is true, so it stops there. Your first condition:

<% if logged_in? %>

Even if they don't have a booth it will never reach the elsif because the first condition is true. You either need:

<% if logged_in? && has_booth?(current_user.id) %>
  // code
<% elsif logged_in? && !has_booth?(current_user.id) %>
  // code
<% else %>
  // code
<% end %>

Or it might be a cleaner approach to separate them into two if/else:

<% if logged_in? %>
  <% if has_booth?(current_user.id) %>
    // code
  <% else %>
    // code
  <% end %>
<% else %>
  // code
<% end %>
like image 65
RichardAE Avatar answered Dec 09 '22 19:12

RichardAE


An even cleaner approach would be to flatten the statement, handling the not logged-in condition first so you don't have to test it along with whether they have a booth:

<% if !logged_in? %>
  // not logged in code
<% elsif has_booth?(current_user.id) %>
  // logged in and has booth code
<% else %> 
  // logged in and does not have booth code
<% end %>

You could also have used unless logged_in?, but the else and elsif don't make as much sense, semantically, with unless and therefore it doesn't read as clearly.

like image 44
Glyde Burdick Avatar answered Dec 09 '22 18:12

Glyde Burdick