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 ?
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.
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.
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 .
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
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With