[英]Strong params not permit dynamic keys
我的控制器正在接收此參數
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"r5KaLCrb1PR//q4HZ0p30dUeK1OHE7cjtoken=",
"servidor"=>
{"nome"=>"USER NAME",
"tipo"=>"1",
"lotacao_ids"=>{"1"=>["", "86", "13"], "2"=>["", "86"], "3"=>["", "66","103","69"]},
"contatos_attributes"=>{"0"=>{"telefone"=>"9229-8396 ", "id"=>"453"}},
"matricula"=>"8741883",
"cpf"=>"16450724870"},
"action"=>"update",
"controller"=>"servidores",
"id"=>"238"}
但是經過之后
params.require(:servidor).permit(:nome, :tipo, :matricula, :cpf, :contatos_attributes=>[:telefone,:id],:lotacao_ids=>{})
造成
{"nome"=>"USER NAME",
"tipo"=>"1",
"matricula"=>"8741883",
"cpf"=>"16450724870",
"contatos_attributes"=>{"0"=>{"telefone"=>"9229-8396 ", "id"=>"453"}},
"lotacao_ids"=>{}}
我怎樣才能收到我PARAMS lotacao_ids
作為lotacao_ids=>{1=>86, 2=>86, 3=>66}
TNKS
與@phoet的幫助回答
def servidor_params
params.require(:servidor).permit(:nome, :tipo, :matricula, :cpf, :contatos_attributes=>[:telefone,:id]).tap do |whitelisted|
whitelisted["lotacao_ids"] = params[:servidor]["lotacao_ids"]
end
end
您想要執行的操作與強參數的想法lotacao_ids
,因為您基本上無法控制lotacao_ids
哈希中的lotacao_ids
。
請注意,如果您在指向散列的鍵中使用允許,則將不允許所有散列。 您還需要指定哈希表中的哪些屬性應列入白名單。
您作為示例給出的參數部分表明這實際上應該是ID數組,而不是哈希表?
無論如何,如果您不關心該字段的內容,您仍然可以將其合並到首先允許填充的哈希中。 它仍然只是一個哈希...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.