1

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?
Community
  • 1
  • 1
Jubl
  • 247
  • 3
  • 14
  • 1
    this might help http://stackoverflow.com/questions/21430151/ruby-on-rails-how-to-get-current-url –  Apr 29 '16 at 00:52

1 Answers1

3

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 %>
Lymuel
  • 574
  • 3
  • 10
  • Ok, so my controller is named "PagesController" and it looks like this: https://puu.sh/ozurk/4f81e9c6ef.png So what you are saying is that "controller_name" would be "PagesController" and the "action_name" would be "get?" If that's the case, how would that point to my homepage? – Jubl Apr 29 '16 at 01:02
  • no the **action_name** will be the `def` you are calling on your controller – Lymuel Apr 29 '16 at 01:04
  • for example, `action_name == "index"` – Lymuel Apr 29 '16 at 01:04
  • and your **controller_name** should be `pages` – Lymuel Apr 29 '16 at 01:05
  • Thank you! I actually adjusted your code a little to "<% if controller_name === "pages" && action_name === "homepage" %>" so it works perfectly now. – Jubl Apr 29 '16 at 01:16
  • Good to hear it helped you – Lymuel Apr 29 '16 at 01:25