2

我有这个数组,我要在 Rails 中返回

[
  ["RETURNS FUEL SURCHARGE", "236.20"],
  ["RETURNS PRINT AND MAIL LABEL", "9.00"],
  ["BILLING ADJUSTMENT FOR W/E 02/23/2013", "7.99"],
  ["SHIPPING CHARGE CORRECTION GROUND", "5471.01"],
  ["BILLING ADJUSTMENT FOR W/E 03/02/2013", "-1.43"],
  ["RETURNS 3 UPS PICKUP ATTEMPTS", "187.50"],
  ["CHARGEBACK CHARGEBACK SURCHARGE", "24.00"],
  ["BILLING ADJUSTMENT FOR W/E 03/16/2013", "-31.02"],
  ["SHIPPING CHARGE CORRECTION OVER MAXIMUM LENGTH", "50.00"],
  ["CHARGEBACK FUEL SURCHARGE", "2.44"]
]

我正在寻找将该信息放在表格中。我对做到这一点的最佳方式有点迷茫。

4

2 回答 2

3

您将需要遍历您的数组并为数组中的每个条目添加一个新的表格行。所以在你看来(假设这个数组被分配给你控制器中的@array变量)你可以像这样循环你的数组:

更新:根据 tadman 的建议添加数组解构

<table>
  <% @array.each do |description, amount| -%>
  <tr>
    <td><%= description %></td>
    <td><%= amount %></td>
  </tr>
  <% end -%>
</table>
于 2013-06-19T18:40:57.567 回答
1

要回答这个问题 -如果我想按数量对数组进行排序,我将如何去做?. 是的,您可以执行以下操作。

array = [["RETURNS FUEL SURCHARGE", "236.20"], ["RETURNS PRINT AND MAIL LABEL", "9.00"], ["BILLING ADJUSTMENT FOR W/E 02/23/2013", "7.99"], ["SHIPPING CHARGE CORRECTION GROUND", "5471.01"], ["BILLING ADJUSTMENT FOR W/E 03/02/2013", "-1.43"], ["RETURNS 3 UPS PICKUP ATTEMPTS", "187.50"], ["CHARGEBACK CHARGEBACK SURCHARGE", "24.00"], ["BILLING ADJUSTMENT FOR W/E 03/16/2013", "-31.02"], ["SHIPPING CHARGE CORRECTION OVER MAXIMUM LENGTH", "50.00"], ["CHARGEBACK FUEL SURCHARGE", "2.44"]]
array.sort_by{|i| i[1].to_i}
# => [["BILLING ADJUSTMENT FOR W/E 03/16/2013", "-31.02"],
#     ["BILLING ADJUSTMENT FOR W/E 03/02/2013", "-1.43"],
#     ["CHARGEBACK FUEL SURCHARGE", "2.44"],
#     ["BILLING ADJUSTMENT FOR W/E 02/23/2013", "7.99"],
#     ["RETURNS PRINT AND MAIL LABEL", "9.00"],
#     ["CHARGEBACK CHARGEBACK SURCHARGE", "24.00"],
#     ["SHIPPING CHARGE CORRECTION OVER MAXIMUM LENGTH", "50.00"],
#     ["RETURNS 3 UPS PICKUP ATTEMPTS", "187.50"],
#     ["RETURNS FUEL SURCHARGE", "236.20"],
#     ["SHIPPING CHARGE CORRECTION GROUND", "5471.01"]]
于 2013-06-19T19:03:23.187 回答