Help with CSS in application.html.erb

I have this code in application.html.erb:   <ul class="glossymenu">     <li class ="current"> <%= link_to _('<b>Repair tickets</b>'), repair_tickets_path%> </li>     <li><%= link_to _('<b>Incidences</b>'), incidences_path%></li>     <li><%= link_to _('<b>Tickets</b>'), tickets_path%></li>     <li><%= link_to _('<b>Clients</b>'), clients_path%></li>     <li><%= link_to _('<b>Products</b>'), products_path %></li>   </ul>

I want that once I have pressed the Incidences link, the li class becomes <li class =" current">, meanwhile the li class from Repair tickets becomes <li>. How can I do that?

John Smith wrote:

I have this code in application.html.erb:   <ul class="glossymenu">     <li class ="current"> <%= link_to _('<b>Repair tickets</b>'), repair_tickets_path%> </li>     <li><%= link_to _('<b>Incidences</b>'), incidences_path%></li>     <li><%= link_to _('<b>Tickets</b>'), tickets_path%></li>     <li><%= link_to _('<b>Clients</b>'), clients_path%></li>     <li><%= link_to _('<b>Products</b>'), products_path %></li>   </ul>

I want that once I have pressed the Incidences link, the li class becomes <li class =" current">, meanwhile the li class from Repair tickets becomes <li>. How can I do that?

I'm going to assume you're using Prototype. If not, I'm sure there is a JQuery equivalent.

Look up addClassName and removeClassName. You will need to create an onclick event handler. I'd probably give each LI an id and pass that into a function, but you could do it in the inline onclick handler if you really wanted to. It certainly wouldn't be DRY.

Peace, Phillip

The way i've always done this is to set a global variable to some value, and check for this in the view. It is messy though so i'm interested to hear any better solutions.

example of what i do:

== controller ==

def some_action   @active_menu_item = "products" end

== view ==   <ul>     <li class="<%= 'current' if @active_menu_item == 'products' %>"> ... </li>   </ul>

You could clean this up my making a helper method for the view, and a before filter in your products controller that sets the active menu item, but this is the only solution I've managed to find.

I wonder if link_to_unless_current might work elegantly for you:

http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000915

You could use CSS to make the plain text in your list correspond to your current "current" style.

-Kyle

Hey that's cool! You also just made me realise that link_to_unless would work quite nicely in different situations as well! :slight_smile: