strong_params 删除 accepts_nested_attributes_for 个模型的 ID

strong_params removing id of accepts_nested_attributes_for models

我有以下 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

但是我的参数和 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"}

您会注意到在参数中,product_grid_location 的 ID 存在,但在 product_grid_params 中被过滤掉了。是什么赋予了?我需要那个 id 来更新嵌套属性。

看起来这是因为 Mongoid 的问题。我传入的 ID 是 Moped::BSON::ObjectId,strong_params 拒绝解析。我将其转换为字符串,之后一切正常:

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