Brian Palmer
8/2/2006 5:00:00 AM
How about using Enumerator#each_slice (or Rails' #in_groups_of):
<html>
<body>
<h1><%= @gallery.name -%></h1>
<table>
<% @gallery.each_slice(4) do |slice| %>
<tr>
<% slice.each do |image| %>
<td><%= image_tag(image) %></td>
<% end %>
</tr>
<% end %>
</table>
</body>
</html>
-- Brian
On Aug 1, 2006, at 10:45 PM, Matt Todd wrote:
> I'll be honest, I think this is Ruby-centric enough because it really
> comes down to basic looping and Erb, so I'll take a stab at it!
>
> <html>
> <body>
> <h1><%= @gallery.name -%></h1>
> <table>
> <% ((@gallery.length / 4) + (((@gallery.length % 4) > 0) ? 1 :
> 0)).downto(1) do %>
> <tr>
> <% 4.downto(1) do %>
> <td><img src='<%= @gallery.shift -%>'></td>
> <% end %>
> </tr>
> <% end %>
> </table>
> </body>
> </html>
>
> That basically figures out how many rows (based on the length divided
> by 4, plus 1 if there were any leftovers) and then loops through them.
> Then, for each row, we loop four times and for each iteration, we
> shift off the front element to go from beginning to end (instead of
> pop, which will pull if off the end).
>
> I like this solution a bit better because it uses a very heirarchical
> structure much like the way the data will be. Plus, I don't have to
> embed any HTML into Erb!
>
> Hope this helps,
>
> M.T.
>