简体   繁体   English

如何在JSON字符串对象中包含rails项目?

[英]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. 我正在尝试将此字符串发送到我的Stripe帐户以处理交易,并且我想同时将要购买的商品的ID和所支付的金额作为JSON字符串发送。 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 模型的一部分是金额,另一部分是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'. 但是,如果您绝对要求idea_id的键为“ id”,则此方法可能无效。 in which case use the first option. 在这种情况下,请使用第一个选项。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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