Topic: Question on how to create a dynamic html table for image gallery

Hello all, I have create a simple photo gallery. Currently I can display one photo per row in my table. I would like to learn how to have 3 or more photos per row in my table. I have tried doing
an inner for loops, using 3.times, but ended up with my photo being copied 3 times per row. Any one have a suggestion on this?

Thanks.

Re: Question on how to create a dynamic html table for image gallery

Try something like this.

<table>
  <tr>
<% for image in @images -%>
    <% count = count + 1 %>
    <td><%= image.show_image %></td>
  <% if ((count % 3) == 0) -%>
  </tr>
  <tr>
  <% end -%>
<% end ->
  </tr>
</table>

That's not the full solution, but it should point you in the right direction.

You might also try scrapping the tables and using floating divs.

Re: Question on how to create a dynamic html table for image gallery

Yes it does thx. Also thanks for the quick responce.

Re: Question on how to create a dynamic html table for image gallery

You can use each_with_index so you don't need to keep track of a separate count variable.

Here's another way you can do it:

<table>
<% @images.in_groups_of(3) do |row| %>
  <tr>
  <% row.each do |image| %>
    <td><%= image.show_image unless image.nil? %></td>
  <% end %>
  </tr>
<% end %>
</table>

Last edited by ryanb (2006-08-22 11:41:53)

Railscasts - Free Ruby on Rails Screencasts