使用bootstrap格式化div中的div

时间:2015-06-14 00:59:55

标签: html ruby-on-rails twitter-bootstrap

总newb问题:我已经创建了一个部分用于调用每行相同div的3个。 div是动态的,所以我觉得我不应该重复相同的代码3次。我怎么能用循环来缩小它?

产品索引

<div class="container">
  <%= render 'products/product' %>
</div>

产品部分

<div class="row">
    <h2 class="text-center">Products</h2>
    <% @products.each do |category, products| %>
      <h3><%= category.name %></h3>
      <% products.each do |product| %>

        <div class="col-md-4">
            <div class="thumbnail">
              <%= image_tag product.default_image.path %>
              </div>
          <div class="caption">
                <h4><%= link_to product.name, product_path(product.permalink) %></h4>
                <p><%= product.short_description %></p>
                <p><b>Price:</b> <%= number_to_currency product.price %></p>
                <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                </div></div>

        <div class="col-md-4">
            <div class="thumbnail">
              <%= image_tag product.default_image.path %>
              <div class="caption">
                <h4><%= link_to product.name, product_path(product.permalink) %></h4>
                <p><%= product.short_description %></p>
                <p><b>Price:</b> <%= number_to_currency product.price %></p>
                <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                </div></div>

          <div class="col-md-4">
            <div class="thumbnail">
              <%= image_tag product.default_image.path %>
              <div class="caption">
                <h4><%= link_to product.name, product_path(product.permalink) %></h4>
                <p><%= product.short_description %></p>
                <p><b>Price:</b> <%= number_to_currency product.price %></p>
                <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                </div></div>
          <% end %>
        </div>
      <% end %>

1 个答案:

答案 0 :(得分:1)

在index.html.erb中:

<h2 class="text-center">Products</h2>
<%= render @products %>`

然后有一个_product.html.erb只有一个产品的信息,rails会知道你想要做什么:

<div class="row">
 <h3><%= product.category.name %></h3>
 <div class="col-md-4">
 <div class="thumbnail">
  <%= image_tag product.default_image.path %>
 </div>
 ....

结帐:http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials。查找子标题:rendering collections