【问题标题】:How do I include rails items in a JSON string object?如何在 JSON 字符串对象中包含 rails 项目?
【发布时间】:2012-07-04 09:02:02
【问题描述】:

我正在尝试将此字符串发送到我的 Stripe 帐户以处理交易,并且我想将所购买商品的 ID 和支付金额作为 JSON 字符串发送。如何在字符串中包含模型的各个部分?

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

模型的一部分是amount,另一部分是idea_id

【问题讨论】:

    标签: ruby-on-rails stripe-payments


    【解决方案1】:

    假设您的模型称为 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”,这可能不起作用。在这种情况下使用第一个选项。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多