2012-08-01 39 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