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

前端 未结 22 1993
一整个雨季
一整个雨季 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:20

    I used a helper to implement this in the style of Rails' form helpers.

    In a helper (e.g. app/helpers/ApplicationHelper.rb):

    def nav_bar
      content_tag(:ul, class: "nav navbar-nav") do
        yield
      end
    end
    
    def nav_link(text, path)
      options = current_page?(path) ? { class: "active" } : {}
      content_tag(:li, options) do
        link_to text, path
      end
    end
    

    Then, in a view (e.g. app/views/layouts/application.html.erb):

    <%= nav_bar do %>
      <%= nav_link 'Home', root_path %>
      <%= nav_link 'Posts', posts_path %>
      <%= nav_link 'Users', users_path %>
    <% end %>
    

    This example produces (when on the 'users' page):

    <ul class="nav navbar-nav">
      <li><a href="/">Home</a></li>
      <li><a href="/posts">Posts</a></li>
      <li class="active"><a href="/users">Users</a></li>
    </ul>
    
    0 讨论(0)
  • 2020-11-27 10:21

    https://github.com/twg/active_link_to

    <%= active_link_to 'Users', users_path, :wrap_tag => :li %>
    

    #=> <li class="active"><a href="/users">Users</a></li>

    0 讨论(0)
  • 2020-11-27 10:21

    You may define a helper method in application_helper.rb

    def create_link(text, path)
      class_name = current_page?(path) ? 'active' : ''
    
      content_tag(:li, class: class_name) do
        link_to text, path
      end
    end
    

    Now you can use like:

    create_link 'xyz', any_path which would render as

    <li class="active">
      <a href="/any">xyz</a>
    </li>
    

    Hope it helps!

    0 讨论(0)
  • 2020-11-27 10:23

    For each link:

    <% if current_page?(home_path) -%><li class="active"><% else -%><li><% end -%>
      <%= link_to 'Home', home_path %>
    </li>
    

    or even

    <li <% if current_page?(home_path) -%>class="active"<% end -%>>
      <%= link_to 'Home', home_path %>
    </li>
    
    0 讨论(0)
  • 2020-11-27 10:23

    I use this for each li:

    <li><%= link_to_unless_current('Home', root_path) { link_to('Home', root_path, class: 'active') } %></li>

    0 讨论(0)
  • 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 %>
    # <a href="/" class="active">Main</a>
    
    <%= active_link_to "Main", root_path, class: "bordered" %>
    # <a href="/" class="bordered active">Main</a>
    
    <%= active_link_to "Main", root_path, class: "bordered", active: "disabled" %>
    # <a href="/" class="bordered disabled">Main</a>
    
    0 讨论(0)
提交回复
热议问题