我正在尝试将我从 CSV 文本输入中解析出来的 ruby 数组呈现为 HTML 表。
解析将输入分离为具有相等列的行数组,从中创建了一个行数组数组。
表示需要容纳行或列的任意组合,并将第一个数组(索引 = 0)视为表头。
这是在我的模型中:
class Size < ActiveRecord::Base
attr_accessor :chart_options, :test
attr_accessible :account_id, :chart, :name
belongs_to :account
def test
'<it>hello</it>'
end
def chart_options
require 'csv'
@chart_options = CSV.parse(chart , {:headers => true, :col_sep => "|", :row_sep => :auto, :skip_blanks => true})
@table = CSV.parse(chart, {:headers => true, :col_sep => "|", :row_sep => :auto, :skip_blanks => true}).to_a
@chart_options
@table
end
end
这是输出:
["UK ", " USA ", " Euro "]
["Small UK ", " Small US ", " Small Euro "]
["Med UK ", " Med US ", " Med Euro "]
["Large UK ", " Large US ", " Large Euro"]
更新的解决方案:
<table class="size_chart table table-striped">
<thead>
<tr>
<% @headers.each do |header| %>
<th><%= header %></th>
<% end %>
</tr>
<thead>
<tbody>
<% @rows.each do |row| %>
<tr>
<% row.each do |column| %>
<td><%= column %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>