Rails 3.2.3 + Twitter Bootstrap + Nav-Tabs: How to show a specific tab?

Marcus Franzen picture Marcus Franzen · Jun 19, 2012 · Viewed 8k times · Source

another newbie-question regarding rails and bootstrap.

I am using something like this:

<div class="tabbable tabs-left">
<ul class="nav nav-tabs">
    <li class="active"><a href="#tab1" data-toggle="tab">About us</a></li>
            <li><a href="#tab9" data-toggle="tab">Address</a></li>
</ul>
<div class="tab-content">
    <div class="tab-pane active" id="tab1">
        <%= render 'about_us' %>
    </div>
    <div class="tab-pane" id="tab9">
        <%= render 'address' %>
    </div>
</div>

My problem is that I render 'address' which includes a form. By submitting this form I end up in an different controller. After saving a new address with this controller I would like to redirect to this page and show the address tab.

The redirect command I tried was: redirect_to salon_path(@salon.id.to_s + "#tab9") This results in calling the url .../salons/1%23tab9. What I think I need its .../salons/1#tab9.

But maybe you are having a better solution how to choose one specific tab.

Using:

gem 'rails', '3.2.3'
gem 'bootstrap-sass', '2.0.3'.

Answer

Marcus Franzen picture Marcus Franzen · Jun 20, 2012

I could figure it out myself, this post here brought me onto the right track: How to get Twitter-Bootstrap navigation to show active link?

<div class="tabbable tabs-left">
<ul class="nav nav-tabs">
    <li class="<%= 'active' if params[:tab] == 'tab1' %>"><a href="#tab1" data-toggle="tab">About us</a></li>
    <li class="<%= 'active' if params[:tab] == 'tab9' %>"> <a href="#tab9" data-toggle="tab">Address</a></li>
</ul>

<div class="tab-content">
    <div class="<%= if (params[:tab] == 'tab1' || !params[:tab])then 'tab-pane active'  else 'tab-pane' end%>" id="tab1">
        <%= render 'about_us' %>
    </div>
    <div class="<%= if params[:tab] == 'tab9' then 'tab-pane active'  else 'tab-pane' end%>" id="tab9">
        <%= render 'address' %>
    </div>
</div>

And in my example I call it like this:

redirect_to salon_path(@salon.id, tab:"tab9")