Best way to add “current” class to nav in Rails 3

后端 未结 24 1754
天涯浪人
天涯浪人 2020-11-29 15:05

I have some static pages in a navigation menu. I want to add a class like \"current\" to the item which is currently displaying.

The way I am doing so is to add tons

24条回答
  •  [愿得一人]
    2020-11-29 15:38

    Yep! Check out this article: A Better Way to Add a ‘selected’ Class to Links in Rails

    Drop nav_link_helper.rb into app/helpers and it can be as easy as:

    <%= nav_link 'My_Page', 'http://example.com/page' %>
    

    The nav_link helper works just like the standard Rails link_to helper, but adds a 'selected' class to your link (or its wrapper) if certain criteria are met. By default, if the link's destination url is the same url as the url of the current page, a default class of 'selected' is added to the link.

    There's a gist here: https://gist.github.com/3279194

    UPDATE: This is now a gem: http://rubygems.org/gems/nav_link_to

提交回复
热议问题