简体   繁体   中英

How do I include rails items in a JSON string object?

I'm trying to send this string to my Stripe account to process a transaction, and I want to send both an ID of the item being bought and the amount paid for as a JSON string. How do I include the pieces of the model in the string?

customer = Stripe::Customer.create(email: email, description: {"amount:" amount, "id:" idea_id}')

One part of the model is amount and the other is idea_id

Assuming your model is called Product

@product = Product.find(params[:id])

customer = Stripe::Customer.create(email: email, description: {amount: @product.amount, id: @product.idea_id}.to_json)

or you can also use

customer = Stripe::Customer.create(email: email, description: @product.as_json(only: [:amount, :idea_id]))

However this may not work if you absolutely require the key for idea_id to be 'id'. in which case use the first option.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM