Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Togglable tabs rails

I want to use the Togglable Tabs from Twitter's Bootstrap on a Rails app, but I can't make it work.

Here is my app/views/pages/home.html.erb :

<ul class="nav nav-tabs" id="dashboard_products">
  <li class="active"><a href="#owned_products"> 1 </a></li>
  <li><a href="#borrowed_products"> 2 </a></li>
  <li><a href="#given_products" > 3 </a></li>
  <li><a href="#requested_products"> 4 </a></li>
</ul>
<div class="tab-content">
  <div class="tab-pane active" id="owned_products"> 1 </div>
  <div class="tab-pane" id="borrowed_products" > 2 </div>
  <div class="tab-pane" id="given_products" > 3 </div>
  <div class="tab-pane" id="requested_products" > 4 </div>
</div>

And here is my app/assets/javascripts/pages.js.coffee :

$ ->
  $('#dashboard_products a').click = (e) ->
    e.preventDefault() 
    $(this).tab('show')

And here my app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .

Where did I fail ?

like image 719
Flo Rahl Avatar asked Jun 13 '13 16:06

Flo Rahl


People also ask

How do you open a specific tab via an external link?

tab=x (where x=tab number) to the URL to display the specific tab. For example: https://demo.dj-extensions.com/dj-tabs/?tab=4 will open 4th tab on the demo site, you can try changing the number to another one to see how it works.

How do I change the active tab color in bootstrap?

You can always modify your navigation list through css. Use this css code instead, in that way use you use the a tag to change the color and its background-color. It's improper to use the li tag to set the background only and a tag to set the color of the text.

How do I make the first tab active in bootstrap?

You can activate a tab or pill navigation without writing any JavaScript code — simply specify the data-bs-toggle="tab" on each tab, or data-bs-toggle="pill" on each pill, as well as create a . tab-pane with unique ID for every tab and wrap them in .


2 Answers

You are missing the data-toggle tag.

Here is an example:

<div class="container-fluid">
    <div class="row-fluid">
        <div class="span8 well">
            <ul class="nav nav-tabs">
                <li class="active"> <a href="#home" data-target="#home" data-toggle="tab">Home</a>

                </li>
                <li><a href="#profile" data-target="#profile" data-toggle="tab">Profile</a>

                </li>
            </ul>
            <div class="tab-content">
                <div class="tab-pane active fade in" id="home">home tab content</div>
                <div class="tab-pane" id="profile">profile tab content</div>
            </div>
        </div>
    </div>
</div>

DEMO

like image 200
Paulo Fidalgo Avatar answered Sep 29 '22 23:09

Paulo Fidalgo


Check if you have included bootstrap js ...and also check you include coffee script file after you include bootstrap js. and coffee script include must also be after your html. as dom must create the element before init tabs..

like image 26
ravisuhag Avatar answered Sep 30 '22 00:09

ravisuhag