From: Brian Palmer Date: 2006-08-02T13:59:51+09:00 Subject: Re: Rails view template for a gallery How about using Enumerator#each_slice (or Rails' #in_groups_of):

<%= @gallery.name -%>

<% @gallery.each_slice(4) do |slice| %> <% slice.each do |image| %> <% end %> <% end %>
<%= image_tag(image) %>
-- 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! > > > >

<%= @gallery.name -%>

> > <% ((@gallery.length / 4) + (((@gallery.length % 4) > 0) ? 1 : > 0)).downto(1) do %> > > <% 4.downto(1) do %> > > <% end %> > > <% end %> >
> > > > 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. >