yalestar
yalestar

Reputation: 9564

Rails: load table cells two across at a time

This seems like it should be easy, but I can't figure out a way to do it. Essentially I want to load (in ERB, from a collection fetched via Rails) an HTML table with cells such that the first row has the first two records, the second row has the next two, etc. Something like this:

-----------
| 1  |  2 |
| 3  |  4 |
| 5  |  6 |
-----------

Seems like there would be a Ruby/Rails way to iterate over a collection two records at a time.

Upvotes: 2

Views: 195

Answers (1)

yalestar
yalestar

Reputation: 9564

Ah, figured it out moments after posting, with help from this question.

For posterity's sake, here's my solution:

  <% @users.each_slice(2) do |two| %>
     <tr>
        <% two.each do |p| %>
           <td>
              <%= p.id %>
           </td>
        <% end %>
     </tr>
  <% end %>

Upvotes: 8

Related Questions