我有一个来自rails应用程序的数组,我得到以下数组。
[
#<InvoiceDetail id: 66018858, invoice_id: 108855, package_id: 21008265, tracking_num: "1Z1Y81420342195332", base_charge: #<BigDecimal:7fed215e7188,'0.857E1',18(18)>, discount: #<BigDecimal:7fed215e70e8,'-0.308E1',18(18)>, created_at: "2012-07-30 15:33:43", updated_at: "2013-02-27 15:31:16", type_id: 54, transaction_date: "2012-07-23", gl_code: nil, division: nil>,
#<InvoiceDetail id: 66018912, invoice_id: 108855, package_id: 21008282, tracking_num: "1Z1Y81420341336904", base_charge: #<BigDecimal:7fed215e5e78,'0.823E1',18(18)>, discount: #<BigDecimal:7fed215e5d88,'-0.274E1',18(18)>, created_at: "2012-07-30 15:33:44", updated_at: "2013-02-27 15:31:16", type_id: 54, transaction_date: "2012-07-23", gl_code: nil, division: nil>,
#<InvoiceDetail id: 66019132, invoice_id: 108855, package_id: 21008350, tracking_num: "1Z1Y81420340443595", base_charge: #<BigDecimal:7fed215e4988,'0.995E1',18(18)>, discount: #<BigDecimal:7fed215e48e8,'-0.428E1',18(18)>, created_at: "2012-07-30 15:33:46", updated_at: "2013-02-27 15:31:16", type_id: 54, transaction_date: "2012-07-24", gl_code: nil, division: nil>,
#<InvoiceDetail id: 66019370, invoice_id: 108855, package_id: 21008425, tracking_num: "1Z1Y81420341794357", base_charge: #<BigDecimal:7fed215ef608,'0.904E1',18(18)>, discount: #<BigDecimal:7fed215ef568,'-0.355E1',18(18)>, created_at: "2012-07-30 15:33:48", updated_at: "2013-02-27 15:31:16", type_id: 54, transaction_date: "2012-07-26", gl_code: nil, division: nil>,
#<InvoiceDetail id: 66018877, invoice_id: 108855, package_id: 21008271, tracking_num: "1Z1Y81420341786795", base_charge: #<BigDecimal:7fed215ee208,'0.736E1',18(18)>, discount: #<BigDecimal:7fed215ee118,'-0.187E1',18(18)>, created_at: "2012-07-30 15:33:43", updated_at: "2013-02-27 15:31:16", type_id: 54, transaction_date: "2012-07-23", gl_code: nil, division: nil>,
#<InvoiceDetail id: 66018894, invoice_id: 108855, package_id: 21008276, tracking_num: "1Z1Y81420341777849", base_charge: #<BigDecimal:7fed215ecd18,'0.823E1',18(18)>, discount: #<BigDecimal:7fed215ecc78,'-0.274E1',18(18)>, created_at: "2012-07-30 15:33:44", updated_at: "2013-02-27 15:31:16", type_id: 54, transaction_date: "2012-07-23", gl_code: nil, division: nil>
]
我想将这些数据解析成一个html表。 Rails还不是很新,也不知道从哪里开始。
编辑:
以下是我希望看到的表格。
<table class="table table-condensed">
<thead>
<tr>
<th>Invoice Id</th>
<th>Package Id</th>
<th>Tracking Number</th>
<th>Base Charge</th>
<th>Discount</th>
<th>Type Id</th>
<th>Transaction Date</th>
<th>Gl Code</th>
<th>Division</th>
</tr>
</thead>
<tbody>
<tr>
<td>108855</td>
<td>21008265</td>
<td>1Z1Y81420342195332</td>
<td>8</td>
<td>-3</td>
<td>54</td>
<td>2012-07-23</td>
<td>nil</td>
<td>nil</td>
</tr>
</tbody>
</table>
答案 0 :(得分:5)
对于相同对象的通用列表,您可以选择(未测试)
之类的内容<table>
<thead>
<tr>
<% @array.first.attributes.each do |attr| %>
<th><%= attr.titlelize %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @array.each do |x| %>
<tr>
<% x.attributes.each do |attr| %>
<td><%= x[attr] %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
或简单使用datagrid gem
ps。我在用户举例说明他希望他的桌子看起来像之前写了这个答案。对于那个确切的表,请查看@ankita answer。
答案 1 :(得分:4)
fotanus的回答是正确的。但是如果您希望列按特定顺序排列,请使用此
<table class="table table-condensed">
<thead>
<tr>
<th>Invoice Id</th>
<th>Package Id</th>
<th>Tracking Number</th>
<th>Base Charge</th>
<th>Discount</th>
<th>Type Id</th>
<th>Transaction Date</th>
<th>Gl Code</th>
<th>Division</th>
</tr>
</thead>
<tbody>
<% @invoice_details.each do |invoice_detail| %>
<tr>
<td><%= invoice_detail.id %></td>
<td><%= invoice_detail.package_id %></td>
<td><%= invoice_detail.tracking_number %></td>
<td><%= invoice_detail.base_charge %></td>
<td><%= invoice_detail.discount %></td>
<td><%= invoice_detail.type_id %></td>
<td><%= invoice_detail.transaction_date %></td>
<td><%= invoice_detail.gl_code %></td>
<td><%= invoice_detail.division %></td>
</tr>
<% end %>
</tbody>
</table>
注意:假设列名称。
答案 2 :(得分:0)
此外,如果您是Rails的新手,您可以制作脚手架并查看它们如何查看文件。