1

I need to wrap 3 results in separate divs. I am returning the results with a basic query:

@items = Item.all

but in the view, I need to list the results in blocks of 3 :

<% @items.each do |item| %>    
<div>
  <p>Result 1</p>
  <p>Result 2</p>
  <p>Result 3</p>
</div>
<div>
  <p>Result 4</p>
  <p>Result 5</p>
  <p>Result 6</p>
</div>

etc...

Any help appreciated from a ROR noob

Aldo 'xoen' Giambelluca
  • 12,075
  • 7
  • 33
  • 39
Neal
  • 63
  • 6

1 Answers1

6
<% @items.each_slice(3) do |chunk| %>
<div>
    <% chunk.each do |item| %>
        <p><%= item %></p>
    <% end %>
</div>
<% end %>

(Related to: How to split (chunk) a Ruby array into parts of X elements?)

Community
  • 1
  • 1
St.Woland
  • 5,357
  • 30
  • 30