How to get Twitter-Bootstrap navigation to show active link?

前端 未结 22 2086
一整个雨季
一整个雨季 2020-11-27 09:31

I\'m not understanding how Twitter Bootstrap does active links for the navigation. If I have a regular navigation like this (with ruby on rails linking):

<         


        
22条回答
  •  离开以前
    2020-11-27 10:24

    I wrote simple helper method using build in view helper current_page? when you can specify custom class name in html_options hash.

    def active_link_to(name = nil, options = nil, html_options = nil, &block)
      active_class = html_options[:active] || "active"
      html_options.delete(:active)
      html_options[:class] = "#{html_options[:class]} #{active_class}" if current_page?(options)
      link_to(name, options, html_options, &block)
    end
    

    Examples (when you are on root_path route):

    <%= active_link_to "Main", root_path %>
    # Main
    
    <%= active_link_to "Main", root_path, class: "bordered" %>
    # Main
    
    <%= active_link_to "Main", root_path, class: "bordered", active: "disabled" %>
    # Main
    

提交回复
热议问题