Rails view template for a gallery

Discussion in 'Ruby' started by stephen O'D, Aug 1, 2006.

  1. stephen O'D

    stephen O'D Guest

    I am totally new to rails, so sorry for the trivial question!

    I have an object, gallery, that contains a list of images. I want to
    display these images in a table with 4 columns and as many rows as it
    takes, closing the final table row at the end.

    What is the best way todo this?

    I can do it (mostly, the final table row is still left open) using the
    following template, but it seems a bit hackish in places:

    <html>
    <body>
    <h1><%= @gallery.name -%></h1>
    <table>
    <%
    i = 1
    for img in @gallery.images
    -%>
    <%= if i == 1 || (i-1)%4 == 0
    "<tr>"
    else
    ""
    end -%>
    <td><img src='<%= img -%>'></td>
    <%= "</tr>" if i%4 == 0 -%>
    <%= i += 1
    "" -%>
    <% end -%>
    </table>
    </body>
    </html>

    Is there a better way?
    stephen O'D, Aug 1, 2006
    #1
    1. Advertising

  2. stephen O'D wrote:
    > I am totally new to rails, so sorry for the trivial question!
    >
    > I have an object, gallery, that contains a list of images. I want to
    > display these images in a table with 4 columns and as many rows as it
    > takes, closing the final table row at the end.
    >
    > What is the best way todo this?
    >
    > I can do it (mostly, the final table row is still left open) using the
    > following template, but it seems a bit hackish in places:
    >
    > <html>
    > <body>
    > <h1><%= @gallery.name -%></h1>
    > <table>
    > <%
    > i = 1
    > for img in @gallery.images
    > -%>
    > <%= if i == 1 || (i-1)%4 == 0
    > "<tr>"
    > else
    > ""
    > end -%>
    > <td><img src='<%= img -%>'></td>
    > <%= "</tr>" if i%4 == 0 -%>
    > <%= i += 1
    > "" -%>
    > <% end -%>
    > </table>
    > </body>
    > </html>
    >
    > Is there a better way?
    >

    Generally, you'll want to ask Rails-specific questions on the Rails
    mailing list, as this is the Ruby mailing list.

    http://lists.rubyonrails.org/mailman/listinfo/rails


    -Justin
    Justin Collins, Aug 2, 2006
    #2
    1. Advertising

  3. stephen O'D

    Matt Todd Guest

    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.
    Matt Todd, Aug 2, 2006
    #3
  4. stephen O'D

    Brian Palmer Guest

    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.
    >
    Brian Palmer, Aug 2, 2006
    #4
  5. stephen O'D

    Matt Todd Guest

    I was writing to my local Ruby user group mailing list about it and it
    dawned on me: I should just partition it! Brian beat me too it, though
    (good thinking). I didn't find documentation on Enumerable#each_slice,
    but I did find Array#slice which should do the same thing (though, I
    just noticed the documentation was for 1.6, so that's probably my
    problem).

    But, in retrospect, I'd do what Brian did: it's short, readable, and
    maybe faster? (I'd have to benchmark it.)

    M.T.
    Matt Todd, Aug 2, 2006
    #5
  6. stephen O'D

    stephen O'D Guest

    Matt Todd wrote:
    > I was writing to my local Ruby user group mailing list about it and it
    > dawned on me: I should just partition it! Brian beat me too it, though
    > (good thinking). I didn't find documentation on Enumerable#each_slice,
    > but I did find Array#slice which should do the same thing (though, I
    > just noticed the documentation was for 1.6, so that's probably my
    > problem).
    >
    > But, in retrospect, I'd do what Brian did: it's short, readable, and
    > maybe faster? (I'd have to benchmark it.)
    >
    > M.T.


    Guys thats great - I new there had to be a tidier way!

    Can I also say thats about the most helpful response I have ever had on
    usenet after posting in the wrong group!

    >From now on I put rails questions in the rails group.


    Cheers,

    Stephen.
    stephen O'D, Aug 2, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    447
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    516
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,075
    Gianni Mariani
    Jun 8, 2007
  4. Parthiv Joshi
    Replies:
    1
    Views:
    632
    Samuel L Matzen
    Jul 6, 2004
  5. Kenneth McDonald
    Replies:
    0
    Views:
    133
    Kenneth McDonald
    Apr 26, 2007
Loading...

Share This Page