Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Austin Klenk
Austin Klenk
4,399 Points

If and Else Statements - RoR

I am trying to make a navigation if and else statement

-----> if a admin is logged in it renders admin login links

-----> if a user is logged in it renders user login links

-----> if customer is logged in it renders customer links

-----? else render guest links

Here is the code that works but when a customer is logged in it works just fine because the else statement for the guest links is right under the customer if statement. when i go to log into the admin or user it still shows the guest links. so if someone could help me out that be fantastic!! here is my current code....-------V

    <% if admin_signed_in? %>
      <%= render '/layouts/admin' %>
    <% end %>

    <% if user_signed_in? %>
      <%= render '/layouts/user' %>
    <% end %>

    <% if customer_signed_in? %>
      <%= render '/layouts/customer' %>

    <% else %>
      <%= render 'layouts/guest' %>

    <% end %>

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You could go with elsif:

<% if admin_signed_in? %>
    <%= render '/layouts/admin' %>
<% elsif user_signed_in? %>
    <%= render '/layouts/user' %>
<% elsif customer_signed_in? %>
    <%= render '/layouts/customer' %>
<% else %>
    <%= render 'layouts/guest' %>
<% end %>

or make a switch statement, something like this:

http://stackoverflow.com/questions/9603321/rails-switch-case-in-the-view

More on the switch itself in pure Ruby:

http://www.codecademy.com/glossary/ruby/switch-statement

Austin Klenk
Austin Klenk
4,399 Points

Awesome that works!!! :)