views:

28

answers:

1

Hi, I trying to display a large set of checkboxes in my rails app and didnt knwo the syntax for displaying like 15 rows then after starting a new column.

I have a model with about 120 entries. Currently, I have it being displayed in the view as....

<% for interest in Interest.find(:all) %>
<%= check_box_tag Blah Blah Blah %>
<%= interest.name %>
<% end %>

How can I make it so it makes a table and after every 15 or so rows make a new column???

A: 

It would be easiest to lay them out in rows instead of columns, because you could use each_slice:

<% Interest.find(:all).each_slice(8) do |interest_row| %>
  <tr>
    <% interest_row.each do |interest| %>
      <td>
        <%= check_box_tag Blah Blah Blah %>
        <%= interest.name %>
      </td>
    <% end %>
  </tr>
<% end %>

but if you have to them in column-major order, you can do

interest_columns = Interest.find(:all).in_groups_of(15)
interest_rows = interest_columns[0].zip(*interest_columns[1..-1]).map(&:compact)

and then do the same double loop over interest_rows

mckeed