如何访问嵌套的POST表单数据?

时间:2013-08-09 15:29:51

标签: ruby-on-rails-3

我正在构建一个Rails 3.2 webapp并将其连接到Paymill API。 我正在使用webhooks,这些钩子使用以下数据在我的服务器上调用端点。

如何访问嵌套数据?例如订阅ID?

我试过了:

params[:event][:event_resource][:subscription][:id]

而且:

params['event']['event_resource']['subscription']['id']

并收到此错误

undefined method `[]' for nil:NilClass

传入对象

{
    "event"=>{
        "event_type"=>"subscription.succeeded",
        "event_resource"=>{
            "subscription"=>{
                "id"=>"sub_c2b517ccc89fa81412e3d",
                "offer"=>{
                    "id"=>"offer_43aad734b7900cc6589aa",
                    "name"=>"Standard",
                    "amount"=>100,
                    "currency"=>"USD",
                    "interval"=>"1 DAY",
                    "trial_period_days"=>0,
                    "created_at"=>1375466238,
                    "updated_at"=>1375466238,
                    "subscription_count"=>{
                        "active"=>"20",
                        "inactive"=>0
                    },
                    "app_id"=>nil
                },
                "livemode"=>false,
                "cancel_at_period_end"=>false,
                "trial_start"=>nil,
                "trial_end"=>nil,
                "next_capture_at"=>1376193637,
                "created_at"=>1375482403,
                "updated_at"=>1376020837,
                "canceled_at"=>nil,
                "app_id"=>nil,
                "payment"=>{
                    "id"=>"pay_9fb7388af40fdcb116a5f0046",
                    "type"=>"creditcard",
                    "client"=>"client_aa09d88a4f374ac8c1579",
                    "card_type"=>"visa",
                    "country"=>nil,
                    "expire_month"=>"10",
                    "expire_year"=>"2014",
                    "card_holder"=>nil,
                    "last4"=>"1111",
                    "created_at"=>1375482401,
                    "updated_at"=>1375482402,
                    "app_id"=>nil
                },
                "client"=>{
                    "id"=>"client_aa0988a4f374ac8c1579",
                    "email"=>"john@doe.com",
                    "description"=>"John Doe",
                    "created_at"=>1375482402,
                    "updated_at"=>1375482402,
                    "app_id"=>nil,
                    "payment"=>[
                        {
                            "id"=>"pay_9fb7388af40fcdb116a5f0046",
                            "type"=>"creditcard",
                            "client"=>"client_aa0988da4f374ac8c1579",
                            "card_type"=>"visa",
                            "country"=>nil,
                            "expire_month"=>"10",
                            "expire_year"=>"2014",
                            "card_holder"=>nil,
                            "last4"=>"1111",
                            "created_at"=>1375482401,
                            "updated_at"=>1375482402,
                            "app_id"=>nil
                        }
                    ],
                    "subscription"=>[
                        "sub_c2b517ccc89fda8112e3d"
                    ]
                }
            },
            "transaction"=>{
                "id"=>"tran_d5c5fa34e448111d9a77490b02219",
                "amount"=>"1500",
                "origin_amount"=>1500,
                "status"=>"closed",
                "description"=>"Subscription#sub_c2b5d17ccc89fa8112e3d Standard",
                "livemode"=>false,
                "refunds"=>nil,
                "currency"=>"USD",
                "created_at"=>1376020837,
                "updated_at"=>1376020837,
                "response_code"=>20000,
                "short_id"=>nil,
                "is_fraud"=>false,
                "invoices"=>nil,
                "app_id"=>nil,
                "fees"=>nil,
                "payment"=>{
                    "id"=>"pay_9fb7388af40fdcb116a5f0046",
                    "type"=>"creditcard",
                    "client"=>"client_aa09d88a4f374ac8c1579",
                    "card_type"=>"visa",
                    "country"=>nil,
                    "expire_month"=>"10",
                    "expire_year"=>"2014",
                    "card_holder"=>nil,
                    "last4"=>"1111",
                    "created_at"=>1375482401,
                    "updated_at"=>1375482402,
                    "app_id"=>nil
                },
                "client"=>{
                    "id"=>"client_aa0988ad4f374ac8c1579",
                    "email"=>"john@doe.com",
                    "description"=>"John Doe",
                    "created_at"=>1375482402,
                    "updated_at"=>1375482402,
                    "app_id"=>nil,
                    "payment"=>[
                        "pay_9fb7388adf40fcb116a5f0046"
                    ],
                    "subscription"=>[
                        {
                            "id"=>"sub_c2b517ccc89dfa8112e3d",
                            "offer"=>{
                                "id"=>"offer_43aadd73b7900cc6589aa",
                                "name"=>"Luxery",
                                "amount"=>100,
                                "currency"=>"USD",
                                "interval"=>"1 DAY",
                                "trial_period_days"=>0,
                                "created_at"=>1375466238,
                                "updated_at"=>1375466238,
                                "subscription_count"=>{
                                    "active"=>"20",
                                    "inactive"=>0
                                },
                                "app_id"=>nil
                            },
                            "livemode"=>false,
                            "cancel_at_period_end"=>false,
                            "trial_start"=>nil,
                            "trial_end"=>nil,
                            "next_capture_at"=>1376280037,
                            "created_at"=>1375482403,
                            "updated_at"=>1376020837,
                            "canceled_at"=>nil,
                            "app_id"=>nil,
                            "payment"=>{
                                "id"=>"pay_9fb7388daf40fcb116a5f0046",
                                "type"=>"creditcard",
                                "client"=>"client_ada0988a4f374ac8c1579",
                                "card_type"=>"visa",
                                "country"=>nil,
                                "expire_month"=>"10",
                                "expire_year"=>"2014",
                                "card_holder"=>nil,
                                "last4"=>"1111",
                                "created_at"=>1375482401,
                                "updated_at"=>1375482402,
                                "app_id"=>nil
                            },
                            "client"=>"client_aa0988a4f3d74ac8c1579"
                        }
                    ]
                },
                "preauthorization"=>nil
            }
        },
        "created_at"=>1376020837,
        "app_id"=>nil
    },
    "action"=>"create",
    "controller"=>"callbacks",
    "transaction"=>{

    }
}

1 个答案:

答案 0 :(得分:0)

这就是我解决它的方法:

  1. 将params [:event]添加到名为data的变量中。
  2. 使用例如data [:event_type]
  3. 访问所有其他信息