# Build a table with content\_tag's

**URL:** <https://www.ruby-forum.com/t/build-a-table-with-content-tags/171616>\
**Category:** Rails\
**Created:** [July 6, 2009, 5:48pm UTC](https://www.ruby-forum.com/t/build-a-table-with-content-tags/171616 "2009-07-06T17:48:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [July 6, 2009, 5:48pm UTC](https://www.ruby-forum.com/t/build-a-table-with-content-tags/171616/1 "2009-07-06T17:48:07Z")

</div>

Hi,

I have a fastcsv collection (2d array) that I want to display as a  
table. I figured the best way would be a helper function, and build  
the table dynamically.

```
table = content_tag(:table,
   #tbody begind
   content_tag(:tbody,
       #tr begin
       collection.collect{|row| content_tag(:tr,
           #td begin/end
           row.collect{|field| content_tag(:td, field)},
       :class => cycle('list-line-odd', 'list-line-even'))} #tr

```

end  
), #tbody end  
:class =\> ‘grid’) #table end

That works really well until I get to the point where I want to add a  
thead or a tfoot. Is the best way to use a helper for each section  
rather than doing the entire table?

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [July 6, 2009, 5:57pm UTC](https://www.ruby-forum.com/t/build-a-table-with-content-tags/171616/2 "2009-07-06T17:57:17Z")

</div>

Joe C. wrote:

> Hi,
> 
> I have a fastcsv collection (2d array) that I want to display as a  
> table. I figured the best way would be a helper function, and build  
> the table dynamically.

The best way would probably be to use a couple of nested partials – one  
for the table as a whole calling one for each row. That way, you don’t  
have to use the awkward syntax of nested content\_tags.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
