CSV テキスト入力から解析した配列の ruby Array を 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>