2010-03-22 127 views
1

什麼是以5行顯示20個圖像列表的最佳方式?或者換句話說,我該如何清理這個醜陋的片段?循環每個x元素

<div class="row"> 
    <% @images.each_with_index do |image, index| %> 
    <% if index != 0 && index % 5 == 0 %> 
     </div><div class="row"> 
    <% end %> 
    <%= image_tag image.url %> 
    <% end %> 
</div> 

回答

3

可以在每個五個圖像的行使用each_slice遍歷圖像:

<% @images.each_slice(5) do |row| %> 
    <div class="row"> 
    <% row.each do |image| %> 
     <%= image_tag image.url %> 
    <% end %> 
    </div> 
<% end %> 
+0

很酷,這是一個更清潔的解決方案。感謝Pär! – Cimm 2010-03-22 10:07:26