简体   繁体   中英

Using bootstrap to create tabs Ruby on rails

I'm starting on ruby on rails and trying to create an app. I have a problem when i trying to create tabs using bootstraps. I want to create tab and each show their content below it for my "Category" attribute but i don't know what can i put in the tag "href" so that i can use that id to display the content below.

<ul class="nav nav-tabs">
<% @categories.each do |category| %>
    <li role="presentation" style="margin:2px; border-radius: 2px; background-color: #CAE5E8"><%= link_to category.content, category %></li>
<% end %>
    <li style="margin:2px; border-radius: 2px; background-color: #CAE5E8; font-size: 10px" role="presentation" ><%= link_to '+', new_category_path %></li>

What you have:

<%= link_to category.content, category %>

What I propose:

<%= link_to category.content, show_category_url(category) %>

This rails url helper generates a link to the show action specific for the category. Alternatively, you could do something like /category/#{category.id} . Both will work if you have your routes.rb and the show action in the CategoriesController set up properly.

You need to add the particular ID of the tabs in each a/link tags

here is how the bootstrap tabs are implemented: Also you can refer the W3 schools to see the live working example :

<ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#home">Home</a></li>
    <li><a data-toggle="tab" href="#menu1">Menu 1</a></li>
    <li><a data-toggle="tab" href="#menu2">Menu 2</a></li>
    <li><a data-toggle="tab" href="#menu3">Menu 3</a></li>
  </ul>

  <div class="tab-content">
    <div id="home" class="tab-pane fade in active">
      <h3>HOME</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    </div>
    <div id="menu1" class="tab-pane fade">
      <h3>Menu 1</h3>
      <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    </div>
    <div id="menu2" class="tab-pane fade">
      <h3>Menu 2</h3>
      <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.</p>
    </div>
    <div id="menu3" class="tab-pane fade">
      <h3>Menu 3</h3>
      <p>Eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
    </div>
  </div>

This would create tabs for you:

<ul class="nav nav-tabs">
 <% @categories.each do |category| %>
  <li href="#<%= category.id %>" role="presentation" style="margin:2px; border-radius: 2px; background-color: #CAE5E8"></li>
<% end %>
<li style="margin:2px; border-radius: 2px; background-color: #CAE5E8; font-size: 10px" role="presentation" ><%= link_to '+', new_category_path %></li>

href should point to the div where you want to keep its content. here I am adding category.id as the element id. So my content divs should look as follows:

<div id = "myTabContent" class = "tab-content">
  <% @categories.each do |category| %>
   <div id="<%= category.id %>" class = "tab-pane fade" >
     <%= link_to category.content, category %>
   </div>
  <% end %>
</div>

If you are looking for specific to bootstrap you need to write two loops

  1. List all the categories in nav-tabs
  2. Render category content

Below is sample code

<ul class="nav nav-tabs">
    -- your each loop do
        <li class="active"><a data-toggle="tab" href="home{{category.id}}">category</a></li>
    --end
</ul>

<div class="tab-content">
-- your each loop do
    <div id="home{{category.id}}" class="tab-pane fade in active">
      <h3>category.Name</h3>
      <p>category.description</p>
    </div>
-- end
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM