Jubl
Jubl

Reputation: 247

Changing Navbar Elements Depending On the Page Using an If Statement

Is there a way to use an if statement to manipulate elements in a navbar based on what page you are on?

For example, what I'm trying to do is that I made my navbar on my homescreen transparent and the link texts are black. If I hover over a navbar link, the link text becomes white with a black background (this is what my class nav-font accomplishes). See below screenshot:

enter image description here However, I want this color scheme to be inverted (or opposite) for all other pages that aren't the home page. Is this currently possible with Rails?

So I want to do the following in my layout file (application.html.erb) using the following pseudocode:

<% if we're on the homepage %>
    <ul class="nav navbar-nav navbar-right nav-font">
        <li><%= link_to "About", about_path %></li>
        <li><%= link_to "Contact", new_contact_path %></li>
    </ul>
<% else %>
    <ul class="nav navbar-nav navbar-right nav-font-inverted">
        <li><%= link_to "About", about_path %></li>
        <li><%= link_to "Contact", new_contact_path %></li>
    </ul>        
<% end %>

What I want to know is:

  1. Is this possible? If so, how might I go about accomplishing this?
  2. If not, is there a simpler solution or alternative?

Upvotes: 1

Views: 1859

Answers (1)

Lymuel
Lymuel

Reputation: 574

try

<% if controller_name == "your controller" && action_name = "your method" %>
    <ul class="nav navbar-nav navbar-right nav-font">
        <li><%= link_to "About", about_path %></li>
        <li><%= link_to "Contact", new_contact_path %></li>
    </ul>
<% else %>
    <ul class="nav navbar-nav navbar-right nav-font-inverted">
        <li><%= link_to "About", about_path %></li>
        <li><%= link_to "Contact", new_contact_path %></li>
    </ul>        
<% end %>

Upvotes: 3

Related Questions