views:

90

answers:

2

In my rails app the model is fetching some XML and returning an array. I want each array item (they are all text typed) to ultimately be a cell in an HTML table.

Does the logic of turning the array elements into the HTML table belong in the controller or the view?

Of course either will work, I'd like your thoughts on best practice.

+4  A: 

The view. Then you can use a different view when you want something besides HTML.

jdl
This is precisely what the view is for - transforming data for presentation for a specific device/purpose.
Toby Hede
+1  A: 

You can add the logic to a helper method:

module ApplicationHelper
  def array_to_html_table data
    col_names = ["Col 1", "Col 2", "Col 3"]
    xm = Builder::XmlMarkup.new(:indent => 2)
    xm.table {
      xm.tr { col_names.each { |key| xm.th(key)}}
      data.each { |row| xm.tr { row.values.each { |value| xm.td(value)}}}
    }
    xm.target
  end
end
KandadaBoggu