Multiple items on 1 column


#1

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

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| %>

<%= image_tag item.image_url, %> <% end %>

I have also tried:

<% for item in @items %>

<%= image_tag item.image_url, %> <% end %>

If I put multiple columns in the same row,

<% for item in @items %>

<%= image_tag item.image_url, %> <%= image_tag item.image_url, %> <%= image_tag item.image_url, %> <% 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


#2

These are images and the image url’s are being pulled from a
database. It seems that

is the trigger to go onto the
next item.
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| -%>
<%= “

” if counter % 3 == 0 -%> <%= image_tag item.image_url -%> <%= "" 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


#3

Jens-Christian F. wrote:

<% counter = 0 -%>
<% @items.each do |item| -%>
<%= “

” if counter % 3 == 0 -%> <%= image_tag item.image_url -%> <%= "" if counter % 3 == 0 -%> <% counter += 1 -%>

<% end -%>

Untested, but clearer (I think :slight_smile: ):

<% while @items.length>0 -%>

<% 3.times do -%> <% if (item = @items.shift) -%> <%= image_tag item.image_url %> <% end -%> <% end -%> <% end -%>


Alex


#4

Jens-Christian and Alex,

Thank you both for the input.

<% The 3.times do %> is exactly what I was looking for.

Dan