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

后端 未结 24 1802
天涯浪人
天涯浪人 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:43

    I use this nav_link(text, link) function in application_helper.rb (Rails 3) to get the job done and it rolls my bootstrap twitter 2.0 nav bar links for me.

    def nav_link(text, link)
        recognized = Rails.application.routes.recognize_path(link)
        if recognized[:controller] == params[:controller] && recognized[:action] == params[:action]
            content_tag(:li, :class => "active") do
                link_to( text, link)
            end
        else
            content_tag(:li) do
                link_to( text, link)
            end
        end
    end
    

    Example:

    <%=nav_link("About Us", about_path) %>
    

提交回复
热议问题