11

使用 Rails 4.0 强参数时,如何允许这样的 JSON?

{
   "user":
   {
       "first_name":"Jello"
   },
   "users_to_employer":[
       {
           "start_date":"2013-09-03T16:45:27+02:00",
           "end_date":"2013-09-10T16:45:27+02:00",
           "employer":{"company_name":"Telenor"}
       },
       {
           "start_date":"2013-09-17T16:45:27+02:00",
           "end_date":null,
           "employer":{"company_name":"Erixon"}
       }
   ]
}

我试过了:

 params.require(:users_to_employers => []).permit(
                                                 :start_date, 
                                                 :end_date => nil,
                                                 :employer => [
                                                     :company_name
                                                 ])

但它没有用。

4

1 回答 1

26

要接受对象数组,请将参数放入数组中:

params.permit(
  users_to_employers: [
    :start_date,
    :end_date,
    employer: [ :company_name ]
  ]
)
于 2013-09-03T17:40:20.123 回答