Reputation: 247
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:
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:
Upvotes: 1
Views: 1859
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