Help with CSS in application.html.erb

I have this code in application.html.erb:

  • <%= link_to _('Repair tickets'), repair_tickets_path%>
  • <%= link_to _('Incidences'), incidences_path%>
  • <%= link_to _('Tickets'), tickets_path%>
  • <%= link_to _('Clients'), clients_path%>
  • <%= link_to _('Products'), products_path %>

I want that once I have pressed the Incidences link, the li class
becomes

  • , meanwhile the li class from Repair
    tickets becomes
  • . How can I do that?
  • John S. wrote:

    I have this code in application.html.erb:

    • <%= link_to _('Repair tickets'), repair_tickets_path%>
    • <%= link_to _('Incidences'), incidences_path%>
    • <%= link_to _('Tickets'), tickets_path%>
    • <%= link_to _('Clients'), clients_path%>
    • <%= link_to _('Products'), products_path %>

    I want that once I have pressed the Incidences link, the li class
    becomes

  • , meanwhile the li class from Repair
    tickets becomes
  • . 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 ==

    • ...

    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.

    On Mar 25, 12:37 pm, John S. [email protected]

    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:

    On Wed, Mar 26, 2008 at 5:33 AM, Kyle [email protected] wrote:

    def some_action
    before filter in your products controller that sets the active menu

    <li><%= link_to _('<b>Tickets</b>'), tickets_path%></li>
    


    Jonathan