Build a table with content_tag's

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?

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
[email protected]