Search code examples
htmlruby-on-railsrubyhtml-tablecontent-tag

Building a HTML table in ruby on rails using the content_tag


I'm trying to build a html table in Ruby On Rails with the help of the content_tag method.

Example:

@template.content_tag(:tr, @template.content_tag(:td, options[:argument0]))

This should render to

<tr><td>content of argument0</td></tr>

What I need is something like

<tr><td>content of argument0</td><td>argument1</td> ... </tr>

Can I build this with the same method? How can I pass two content_tags?


Solution

  • You can use concat :

     @template.content_tag(:tr, 
        @template.content_tag(:td, options[:argument0]).
        concat(@template.content_tag(:td, options[:argument1])).
        concat(@template.content_tag(:td, options[:argument2]))
        # ....
     )
    

    Or using a loop - as OP suggested in the comments

    rows = returning "" do |cells|
       5.times do |i|
         cells.concat @template.content_tag(:td, options["argument#{i}".to_sym]
       end
    end
    @template.content_tag(:tr,rows)