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):
<
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');
});
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>
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.
you could use tabulous for the links
article here on how to combine tabulous with twitter bootstrap and rails 3.x
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>
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