kac*_*lme 4 html css ruby-on-rails twitter-bootstrap twitter-bootstrap-3
我在带有引导程序的rails中.
我有一个产品网站,有很多产品.我需要以"网店前面"的网格格式显示我的产品.每个产品都有照片和一些信息.
我试图使用product.each迭代每个产品,同时尝试使用bootstrap的网格系统.
随着时间的推移,我将需要添加其他产品,因此需要每个产品从一行流到下一行.我没有在bootstrap之外使用任何额外的CSS样式.
截至目前,我看来每个产品都在下一行,并且每个产品都在它自己的列中,但是我为了调整图像或每个产品所在的div或col的任何尝试,一切都变得不对齐.
这是我的html.erb文件:
<div class="row">
<% @products.each do |product| %>
<div class="col-md-3">
<div id="storeimages">
<%= image_tag(product.image_url, class: "img-responsive") %>
<h3><%= product.title %></h3>
<div><%= sanitize(product.description) %></div>
<div><%= number_to_currency(product.price) %></div>
<%= button_to 'Add to Cart', line_items_path(product_id: product),
class: 'btn btn-info btn-sm', remote: true %>
</div>
</div>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
Jon*_*Jon 13
假设您想要4列的行,您可以这样做:
<% @products.in_groups_of(4, false).each do |group| %>
<div class='row'>
<% group.each do |product| %>
<div class='col-md-3'>
<%= image_tag(product.image_url, class: "img-responsive") %>
<h3><%= product.title %></h3>
<div><%= sanitize(product.description) %></div>
<div><%= number_to_currency(product.price) %></div>
<%= button_to 'Add to Cart', line_items_path(product_id: product), class: 'btn btn-info btn-sm', remote: true %>
</div>
<% end %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这会将您的数据集拆分为4个项目组,然后为每个组输出一个行div.然后在每一行中,它在其自己的列div中输出该组的每个成员.
在false通过了以in_groups_of确保您不尝试输出列一排,其中有少于4项.如果您的产品总数不能被4整除,那么这将发生在您的最后一行,因为默认情况下该函数将填充数组nil.
感谢@vermin fill_with提示!
| 归档时间: |
|
| 查看次数: |
2225 次 |
| 最近记录: |