0

我有一个来自 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>
4

3 回答 3

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 答案。

于 2013-05-02T01:25:32.827 回答
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>

注意:假定列名。

于 2013-05-02T06:09:13.690 回答
0

此外,如果您是 Rails 新手,您可以制作脚手架并查看他们是如何查看文件的。

于 2013-05-02T11:55:02.637 回答