每6个滑轨插入每个循环插入标签? [英] Rails each loop insert tag every 6 items?
问题描述
我有X个图像对象,我需要在视图中循环通过这些图像对象,并且想要每6个对象(对于一个画廊)创建一个新的div.
I have X number of image objects that I need to loop through in a view and want to create a new div every 6 objects or so (for a gallery).
我已经看过周期,但是似乎每隔一个记录都会改变.有谁知道一种每6次将代码插入视图中的方法吗?
I have looked at cycle but it seems to change every other record. Does anyone know of a way to insert code into a view every 6 times?
我可能可以使用嵌套循环来做到这一点,但我对此颇为困惑.
I could probably do it with nested loops but I am kinda stumped on this one.
推荐答案
您可以将Enumerable#each_slice
与#each
结合使用以避免内联计算. each_slice
将数组分成n个块,在这种情况下为6.
You can use Enumerable#each_slice
in conjunction with #each
to avoid inline calculations. each_slice
breaks the array into chunks of n, in this case 6.
<% @images.each_slice(6) do |slice| -%>
<div class="gallery">
<% slice.each do |image| -%>
<%= image_tag(image.url, :alt => image.alt) %>
<% end -%>
</div>
<% end -%>
这篇关于每6个滑轨插入每个循环插入标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!