2

I have this array I'm returning in 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"]
]

I'm looking to place that info in table. I'm a little lost on best way to do that.

2 Answers 2

3

You will need to iterate through your array and add a new table row for each entry in the array. So in your view (assuming this array is assigned to an @array variable in your controller) you could loop through your array like so:

Update: add array destructuring per tadman's suggestion

<table>
  <% @array.each do |description, amount| -%>
  <tr>
    <td><%= description %></td>
    <td><%= amount %></td>
  </tr>
  <% end -%>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

It's also possible to label these like do |description, amount|.
If I wanted to sort the array by amount how would I go about doing that?
1

To answer the question - If I wanted to sort the array by amount how would I go about doing that?. yes you can do as below.

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"]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.