You can use each_slice method:
<% @items.each_slice(3) do |array_of_3_items|%>
<% array_of_3_items.each do |item|%>
<%= item.content %>
<% end %>
<% end %>
Additional note: What if you wanted 3 div for the last row, even if the last row contains only 2 items? (i.e. the @items
is a list of 5 items, the last row should output only 2 div).
The solution:
<% @items.each_slice(3) do |array_of_3_items|%>
<% array_of_3_items.each do |item|%>
<%= item.content %>
<% end %>
<% (3 - array_of_3_items.length).times.each do |fake_div| %>
<% end %>
<% end %>