Bootstrap navigation tabs with rails subpages

I'm attempting to style a rails app with bootstrap navigation tabs. My App is such that I have not only pages but subpages as well. Tabs represent superpages (so to speak). See below:

<ul class="nav nav-tabs">
    <li class="tab"><%= link_to "Contacts", edit_entities_path(@company) %></li>
    <% for stock in @company.stocks.each %>
        <% if stock.id %>
            <li class="tab"><%= link_to "#{stock.security_series} #{stock.security_class} Stock", stock_stockholders_path(stock) %></li>
        <% end %>
    <% end %>
    <% for option in @company.options.each %>
        <% if option.id %>
            <li class="tab"><%= link_to "#{option.security_series} #{option.security_class} Options", option_optionees_path(option) %></li>
        <% end %>
    <% end %>
    <% for warrant in @company.warrants.each %>
        <% if warrant.id %>
            <li class="tab"><%= link_to "#{warrant.security_series} #{warrant.security_class} Warrant", warrant_warrantees_path(warrant) %></li>
        <% end %>
    <% end %>
    <% for note in @company.convertible_notes.each %>
        <% if note.id %>
            <li class="tab"><%= link_to "#{note.security_series} #{note.security_class} Note", convertible_note_convertible_notees_path(note) %></li>
        <% end %>
    <% end %>
</ul>

Tabs denote any number of stocks, convertible_notes, options, and warrants

resources :stocks do
    resources :stockholders
end
resources :convertible_notes do
    resources :convertible_notees
end


resources :options do
    resources :optionees
end

resources :warrants do
    resources :warrantees
end

For more context a Company has_many :stocks :convertible_notes :options :warrants

What I'm after: I would like to apply the active class if I am on a given page OR if I'm on its sub-page. So, I not only want the stock with ID 1's tab to be .active if I'm on it, but also if I'm on the stockholder subpage.

What might be the best way to accomplish this? Thanks in advance.

Answers


Figured it out.

Basically for each item, I created a helper method to compare the loaded parameters ids with the id for each model in each loop. So take stocks for example, in the view, I added the helper in classes:

<% for stock in @company.stocks.each %>
    <% if stock.id %>
        <li class="tab <%= stock_nav(params, stock.id) %>"><%= link_to "#{stock.security_series} #{stock.security_class} Stock", stock_stockholders_path(stock) %></li>
    <% end %>
<% end %>

I added the helper to my application helper:

def stock_nav(params, stock)
    "active" if params[:controller]=="stockholders" && stock.to_s==params[:stock_id].to_s
end

(As it happens both pages are run off the stockholders controller, which I should have mentioned in the question) If they weren't one would just add another if statement to check the controller and then the parameters but it woudl follow the same basic outline.


Need Your Help

I get a NullPointerException when trying the Eclipse "Hello World" tutorial

java android eclipse sdk adt

I was finishing up the "Hello World" tutorial in Eclipse (with the ADT plugin) and apparently, you are supposed to rename the string resource "hello" with "Hello Android, I am a string resource". W...

Dynamically creating a new set for each iteration of a while loop

python sql loops set

I would like to create a unique set for each iteration of a while loop. This while loop is nested inside of a for loop that does epoch conversions, etc., and creates a list that has the values asso...