Ruby on Rails, Array to HTML table, controller or view?
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 logi开发者_运维百科c 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.
The view. Then you can use a different view when you want something besides HTML.
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
精彩评论