ruby-on-railstwitter-bootstrap-rails

Active class did not set to menu_item if url have '?' sign in twitter-bootstrap-rails gem


I using gem https://github.com/seyhunak/twitter-bootstrap-rails/ and navbar helpers from them to build header.

For ex. i have a users link in my header. And if current url is '/users', users link sets active and highlighted correcrly. But if current url is '/users?top10=true', users link did not set active. Is there right behavior? Because html page for '/users' and /users?top10=true' is the same page with different content.

Is there bug or feature?


Solution

  • Bootstrap will only consider link as active if and only if path will match with href's value. It don't have support for query strings yet. You can use following code options to make links active/non-active with custom code:

    <ul class="nav">
      <li class="<%= 'active' if controller_name == 'controller1' %>">
        <%= link_to "Link1", "/link1" %>
      </li>
      <li class="<%= 'active' if controller_name == 'controller2' %>">
        <%= link_to "Link2", "/link2" %>
      </li>
      <li class="<%= 'active' if controller_name == 'controller3' %>">
        <%= link_to "Link3", "/link3" %>
      </li>        
    </ul>
    

    Or you can use jQuery:

    var url = window.location;
    
    $('ul.nav a').filter(function() {
        return this.href == url;
    }).parent().addClass('active');