我有以下strong_params声明:
Sub Macro1()
' Macro1 Macro
Selection.ApplyNames Names:=Array ("ATL_BV_XP", "ATL_BV_XP..EUN", "ATL_PK_XP", _
"ATL_PK_XP..EUN", "CHN_PK_IM", "CHN_PK_IM..EUN", "CHN_PK_IM..SHREUN", _
"E15_AG_AH", "E15_AG_EPA", "E15_AG_SFP", "E15_AG_SFP..CF" _
, "E15_APF_FE"), IgnoreRelativeAbsolute:=True, UseRowColumnNames:=True, _
OmitColumn:=True, OmitRow:=True, Order:=1, AppendLast:=False
End Sub
但我的params和product_grid_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
您会注意到,在参数中,(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"}
的ID已存在,但会在product_grid_params中过滤掉。是什么赋予了?我需要那个id来更新嵌套属性。
答案 0 :(得分:0)
看起来这是因为Mongoid存在问题。我传入的id是Moped::BSON::ObjectId
,strong_params拒绝解析。我将它转换为字符串,之后一切都很好:
params[:product_grid][:product_grid_locations_attributes].each { |location| location[:id] = location[:id].to_str }