2012-08-01 18 views
1

新手,並且真的很感謝一些幫助渲染3個對象的對象。一次呈現n個對象,插入代碼,然後重複接下來的n個對象 - Rails

我目前顯示對象的列表與渲染:

<%= render @gifts %> 

每個對象都有一些特點,從部分_gift.html.erb

這一切工作正常渲染。但是,如何循環遍歷所有使用div包裝每個3個對象的@gifts對象?理想的結果是一樣的東西:

<div class="row-fluid> 
# The first 3 objects from @gifts 
</div> 

<div class="row-fluid> 
# The next 3 objects from @gifts 
</div> 

回答

3
<% @gifts.each_slice(3) do |slice| %> 
    <div class="row-fluid> 
    # slice now is a 3 element array, iterate over it and render as you see fit 
    </div> 
<% end %> 

我更多的是HAML的傢伙,但這應該工作。

+0

那完美。非常感謝。 – 2012-08-01 19:57:47