ruby - Rails and Stripe Line Items - Stack Overflow

admin2025-04-21  3

I've integrated Stripe Tax into my application but I can't get past an error when I try to loop my line items (products) into the code for the Tax Calculation. It DOES NOT give me an error if I do the following and only include one line item.

line_items: [
  {
    amount: self.subtotal,
    reference: 'Products',
  },
],

This is the error it gives when I do the code below:

received unknown parameter: v1

I'd like to send all of the line items from the cart the customer is purchasing. I have the syntax wrong or something. I don't think it likes the comma.

line_items: [
  @cart.cart_line_items.each do |item|
    {
      amount: item.subtotal,
      reference: item.name,
    },
  end
],

Any thoughts or advice is appreciated.

I've integrated Stripe Tax into my application but I can't get past an error when I try to loop my line items (products) into the code for the Tax Calculation. It DOES NOT give me an error if I do the following and only include one line item.

line_items: [
  {
    amount: self.subtotal,
    reference: 'Products',
  },
],

This is the error it gives when I do the code below:

received unknown parameter: v1

I'd like to send all of the line items from the cart the customer is purchasing. I have the syntax wrong or something. I don't think it likes the comma.

line_items: [
  @cart.cart_line_items.each do |item|
    {
      amount: item.subtotal,
      reference: item.name,
    },
  end
],

Any thoughts or advice is appreciated.

Share Improve this question edited Feb 2 at 22:54 codename_duchess 9515 silver badges9 bronze badges asked Jan 23 at 19:24 Tony S.Tony S. 1,2832 gold badges10 silver badges13 bronze badges 4
  • 3 Use #map instead of #each and get rid of the brackets and comma. #each returns the object it was called on and not the results of the block and should only be used if you just care about the side effects and not the results. This is more a Ruby 101 thing than a Stripe thing. – max Commented Jan 23 at 21:00
  • @max I switched to map as you suggested. I taught myself Rails so I didn't start with Ruby. I've seen map many times of course but never used it until now. Here's what I used in place of the above: line_items: self.cart_line_items.map {|item| {:amount => item.price, :reference => item.product.name}},. Thanks for the help. – Tony S. Commented Jan 24 at 3:27
  • Better. But the JSON style hash syntax is generally preferred over hashrockets in modern Ruby when writing hashes with symbols for keys. { amount: item.price, reference: item.product.name }. It's more succint and less noise. – max Commented Jan 24 at 9:39
  • You're right. I changed to this way. Sometimes I get caught up in the way I used to do things. Again, appreciate the education. – Tony S. Commented Jan 24 at 17:41
Add a comment  | 

1 Answer 1

Reset to default 1

Your issue is with how you're attempting to use .each inside an array. In Ruby, .each does not return an array—it just iterates over the items. Instead, you should use .map, which returns a new array.

line_items: @cart.cart_line_items.map do |item|
  {
    amount: item.subtotal,
    reference: item.name
  }
end

What Went Wrong?

  1. Misuse of .each

    • .each only iterates and does not return an array.

    • You need .map, which transforms and returns a new array.

  2. Syntax Issue

    • Your current syntax is wrapping .each inside an array (line_items: [ @cart.cart_line_items.each do ... end ]), which results in an unexpected behavior.

Why This Works?

  • .map loops through @cart.cart_line_items and returns an array of hashes, correctly formatting line_items.
转载请注明原文地址:http://anycun.com/QandA/1745179766a90405.html