我正在尝试将此字符串发送到我的 Stripe 帐户以处理交易,并且我想将所购买商品的 ID 和支付金额作为 JSON 字符串发送。如何在字符串中包含模型的各个部分?
customer = Stripe::Customer.create(email: email, description: {"amount:" amount, "id:" idea_id}')
模型的一部分是数量,另一部分是idea_id
我正在尝试将此字符串发送到我的 Stripe 帐户以处理交易,并且我想将所购买商品的 ID 和支付金额作为 JSON 字符串发送。如何在字符串中包含模型的各个部分?
customer = Stripe::Customer.create(email: email, description: {"amount:" amount, "id:" idea_id}')
模型的一部分是数量,另一部分是idea_id
假设您的模型称为 Product
@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”,这可能不起作用。在这种情况下使用第一个选项。
希望这可以帮助