[英]How do I include rails items in a JSON string object?
我正在嘗試將此字符串發送到我的Stripe帳戶以處理交易,並且我想同時將要購買的商品的ID和所支付的金額作為JSON字符串發送。 如何在字符串中包含模型的各個部分?
customer = Stripe::Customer.create(email: email, description: {"amount:" amount, "id:" idea_id}')
模型的一部分是金額,另一部分是idea_id
假設您的模型稱為產品
@product = Product.find(params[:id])
customer = Stripe::Customer.create(email: email, description: {amount: @product.amount, id: @product.idea_id}.to_json)
或者你也可以使用
customer = Stripe::Customer.create(email: email, description: @product.as_json(only: [:amount, :idea_id]))
但是,如果您絕對要求idea_id的鍵為“ id”,則此方法可能無效。 在這種情況下,請使用第一個選項。
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.