Rails创建一个同时接受多个对象的端点的方法



我需要创建一个同时接受多个对象的端点。下面的请求示例(json主体):

{
    "obd_bluetooth_events" : [
        {
            "car_id": 1,
            "obd_plugin_id": "1",
            "kind": "CONNECT",
            "date": 1422369149
        },
        {
            "car_id": 1,
            "obd_plugin_id": "1",
            "kind": "DISCONNECT",
            "date": 1422369149
        },
        {
            "car_id": 1,
            "obd_plugin_id": "1",
            "kind": "CONNECT",
            "date": 1422369149
        }
    ]
}

所以为了能够通过数组创建方法:@obd_bluetooth_event.create(obd_bluetooth_events_params)

我需要定义obd_bluetooth_events_params方法,如下所示:

def obd_bluetooth_events_params
  params.permit(
    obd_bluetooth_events: [
      :car_id, 
      :obd_plugin_id, 
      :kind, 
      :date
    ]
  )[:obd_bluetooth_events]
end

打电话后我得到:

Unpermitted parameters: obd_bluetooth_event
=> [{"car_id"=>1, "obd_plugin_id"=>"1", "kind"=>"CONNECT", "date"=>1422369149},
 {"car_id"=>1, "obd_plugin_id"=>"1", "kind"=>"DISCONNECT", "date"=>1422369149},
 {"car_id"=>1, "obd_plugin_id"=>"1", "kind"=>"CONNECT", "date"=>1422369149}]

我想知道是否有一种更严厉的方式来允许一系列物体?

def obd_params params.require(:myparams).permit(:myarray => []) end

允许数组对我来说很好。希望这能有所帮助;)

相关内容

  • 没有找到相关文章

最新更新