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

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

    Use this instead to select active link in nav based on the current route without server code:

        $(document).ready(function () {
            $('a[href="' + this.location.pathname + '"]').parent().addClass('active');
        });
    
    0 讨论(0)
  • 2020-11-27 10:08

    Just made an answer on the very same question here Twitter Bootstrap Pills with Rails 3.2.2

    <ul class="nav">
      <li class="<%= 'active' if params[:controller] == 'controller1' %>"> <a href="/link">Link</a> </li>
      <li class="<%= 'active' if params[:controller] == 'controller2' %>"> <a href="/link">Link</a> </li>
      <li class="<%= 'active' if params[:controller] == 'controller3' %>"> <a href="/link">Link</a> </li>        
    </ul>
    
    0 讨论(0)
  • 2020-11-27 10:08

    Today I had the same question/problem but with an other approach for the solution. I create a helper function in application_helper.rb:

    def navMainAktiv(actionName)
        if params[:action] == actionName    
        "active"
        end
    end
    

    and the link looks like this:

    <li class="<%= navMainAktiv('about')%>"><%= link_to "About", about_path %></li>
    

    You can replace params[:action] with params[:controller] and set your controller name in the link.

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

    you could use tabulous for the links

    article here on how to combine tabulous with twitter bootstrap and rails 3.x

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

    Here's what I did:

    I created a ViewsHelper and included in ApplicationController:

    include ViewsHelper
    

    Inside ViewsHelper I created a simple method like this:

    def page_state(path)
      current_page?(path) ? 'active' : ''
    end
    

    In my view I do this:

    <li class="<%= page_state(foobar_path) %>"><%= link_to 'Foobars', foobar_path %></li>
    
    0 讨论(0)
  • 2020-11-27 10:09
      def active_navigation?(controllers_name, actions_name)
       'active' if controllers_name.include?(controller_name) && actions_name.include?(action_name)
      end
    

    slim

    li class=(active_navigation?(['events'], ['index', 'show'])) = link_to t('navbar.events'), events_path
    
    0 讨论(0)
提交回复
热议问题