Hello, I am trying to put multiple items in one column and can''t seem to get the syntax right. These are images and the image url''s are being pulled from a database. It seems that <tr></tr> is the trigger to go onto the next item. Here is the code from the controller: def list @items = Item.find_all end Here is the code from list.rhtml file: <% @items.each do |item| %> <tr> <td> <%= image_tag item.image_url, %> </td> </tr> <% end %> I have also tried: <% for item in @items %> <tr> <td> <%= image_tag item.image_url, %> </td> </tr> <% end %> If I put multiple columns in the same row, <% for item in @items %> <tr> <td> <%= image_tag item.image_url, %> </td> <td> <%= image_tag item.image_url, %> </td> <td> <%= image_tag item.image_url, %> </td> </tr> <% end %> it puts the same image 3 times and then the next row is the next item 3 times until the next row. How do I get 3 different images on the same row? Any code examples would be greatly appreciated. TIA Dan
> > These are images and the image url''s are being pulled from a > database. It seems that <tr></tr> is the trigger to go onto the > next item.<tr> is the "table row" tag of html, that creates a new row in your table. try the following in your view: <% counter = 0 -%> <% @items.each do |item| -%> <%= "<tr>" if counter % 3 == 0 -%> <td> <%= image_tag item.image_url -%> </td> <%= "</tr>" if counter % 3 == 0 -%> <% counter += 1 -%> <% end -%> that will increment a counter and put in the table rows every time, the counter reaches a number that is divisible by 3 ( % is the modulo function ) hth jc -- InVisible GmbH, Langgr?tstrasse 172, 8047 Z?rich Phone: +41 44 401 09 30 http://www.invisible.ch http://not.invisible.ch -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2361 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060110/46227ee9/smime.bin
Jens-Christian Fischer wrote:>> >> These are images and the image url''s are being pulled from a >> database. It seems that <tr></tr> is the trigger to go onto the next >> item. > > > <tr> is the "table row" tag of html, that creates a new row in your table. > > try the following in your view: > > <% counter = 0 -%> > <% @items.each do |item| -%> > <%= "<tr>" if counter % 3 == 0 -%> > <td> > <%= image_tag item.image_url -%> > </td> > <%= "</tr>" if counter % 3 == 0 -%> > <% counter += 1 -%> > > <% end -%> >Untested, but clearer (I think :-) ): <% while @items.length>0 -%> <tr> <% 3.times do -%> <% if (item = @items.shift) -%> <td><%= image_tag item.image_url %></td> <% end -%> <% end -%> </tr> <% end -%> -- Alex