简体   繁体   English

Ruby on Rails,数组到HTML表,控制器或视图?

[英]Ruby on Rails, Array to HTML table, controller or view?

In my rails app the model is fetching some XML and returning an array. 在我的rails应用程序中,模型正在获取一些XML并返回一个数组。 I want each array item (they are all text typed) to ultimately be a cell in an HTML table. 我希望每个数组项(它们都是文本类型的)最终成为HTML表中的单元格。

Does the logic of turning the array elements into the HTML table belong in the controller or the view? 将数组元素转换为HTML表的逻辑是否属于控制器或视图?

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. 然后,当您需要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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM