views:

48

answers:

3

Currently this just makes a long list. How do I display this with 5 records per row instead of 1?

<% @tags.each do |tag| %>

<p><%= tag_search(tag) %></p>

<% end %>

currently

tag1
tag2
tag3
tag4
tag5
tag6
tag7
...

desired

tag1 tag2 tag3 tag4 tag5
tag6 tag7

I know this is really basic but I just cannot manage to find the right google search terms to get the answer on my own. Thanks!

this is what I ended up using

<table>
<% @tags.in_groups_of(4, false) do |row_tag| %>
  <tr>
    <% for tag in row_tag %>
      <td><%= tag_search(tag) %></td>
    <% end %>
  </tr>
<% end %>
</table>
+1  A: 

There is probably a better way to do it but this will work:

  <% @tags.each do |tag, i| %>
         <%= '<p>' if i == 1 %>
         <%= tag_search(tag) %>
         <% if i == 5 %>
             <% i = 0 %>
             </p>
         <% end %>
 <% end %>
Sam
Please avoid setting variables in the view.
Simone Carletti
+5  A: 

You can use the ActiveSupport method in_groups_of to take an array and put it into groups

<% @tags.in_groups_of(5).each do |tag_array| %>
  <% tag_array.each |tag| %>
  ...

Original Doc from Rails Docs

%w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g}
  ["1", "2", "3"]
  ["4", "5", "6"]
  ["7", nil, nil]
Jesse Wolgamott
perfect, thanks
Jason
+2  A: 
<% @tags.each_with_index do |tag, index| %>
  <%= tag_search(tag) %>
  <%= "<br />" if (index % 5).zero? %>
<% end %>

You can also use ActiveSupport's in_group_of.

Simone Carletti