1

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.

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is precisely what the view is for - transforming data for presentation for a specific device/purpose.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.