# Multiple items on 1 column

**URL:** <https://www.ruby-forum.com/t/multiple-items-on-1-column/60702>\
**Category:** Rails\
**Created:** [January 10, 2006, 8:23am UTC](https://www.ruby-forum.com/t/multiple-items-on-1-column/60702 "2006-01-10T08:23:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 10, 2006, 8:23am UTC](https://www.ruby-forum.com/t/multiple-items-on-1-column/60702/1 "2006-01-10T08:23:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 10, 2006, 9:29am UTC](https://www.ruby-forum.com/t/multiple-items-on-1-column/60702/2 "2006-01-10T09:29:42Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 10, 2006, 9:50am UTC](https://www.ruby-forum.com/t/multiple-items-on-1-column/60702/3 "2006-01-10T09:50:50Z")

</div>

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 🙂 ):

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

 \<% 3.times do -%\> \<% if (item = @items.shift) -%\> \<%= image\_tag item.image\_url %\> \<% end -%\> \<% end -%\> \<% end -%\>

–  
Alex

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 10, 2006, 10:18am UTC](https://www.ruby-forum.com/t/multiple-items-on-1-column/60702/4 "2006-01-10T10:18:05Z")

</div>

Jens-Christian and Alex,

Thank you both for the input.

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

Dan
