简体   繁体   English

strong_params删除模型的accepts_nested_attributes_for的ID

[英]strong_params removing id of accepts_nested_attributes_for models

I have the follow strong_params statement: 我有以下strong_params语句:

  def product_grid_params
    params.require(:product_grid).permit(:name,
      product_grid_locations_attributes: [:id, :grid_index, :item_id, :item_type, :short_name, :long_name]
    ).merge({ venue_id: params[:venue_id] })
  end

But my params and product_grid_params look like this: 但是我的参数和product_grid_params看起来像这样:

(byebug) params
{"product_grid"=>{"product_grid_locations_attributes"=>[{"id"=>"5560d1f7a15a416719000007", "short_name"=>"shrt", "long_name"=>"Whiskey Ginger", "grid_index"=>73, "item_type"=>"product", "item_id"=>"9b97aa28-1349-4f60-a359-3907c8ac9a74"}]}, "id"=>"5560d1f7a15a416719000006", "venue_id"=>"5560d1f7a15a416719000005", "format"=>"json", "controller"=>"api/v2/manager/product_grids", "action"=>"update"}
(byebug) product_grid_params
{"product_grid_locations_attributes"=>[{"grid_index"=>73, "item_id"=>"9b97aa28-1349-4f60-a359-3907c8ac9a74", "item_type"=>"product", "short_name"=>"shrt", "long_name"=>"Whiskey Ginger"}], "venue_id"=>"5560d1f7a15a416719000005"}

You'll notice that in the params, the product_grid_location 's id is present, but it gets filtered out in product_grid_params. 您会注意到,在参数中,存在product_grid_location的id,但是在product_grid_params中将其过滤掉了。 What gives? 是什么赋予了? I need that id there to update nested attributes. 我在那里需要该ID来更新嵌套属性。

Looks like this was because of an issue with Mongoid. 看起来这是由于Mongoid出现问题。 The id I was passing in was a Moped::BSON::ObjectId , which strong_params refused to parse. 我传入的id是Moped::BSON::ObjectId ,strong_params拒绝解析。 I converted it to a string and everything was fine after that: 我将其转换为字符串,之后一切都很好:

params[:product_grid][:product_grid_locations_attributes].each { |location| location[:id] = location[:id].to_str }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM