![](/img/trans.png)
[英]strong_params removing id of accepts_nested_attributes_for models
[英]strong_params: Handling Dynamic Number of Nested Objects/Attributes
我有一個模型Foo,它可以接受模型欄的嵌套屬性。 對表單中的嵌套模型使用rails約定, form_for
和fields_for
創建表單的params
哈希:
{...
:foo => {
:name => "Lunes",
:bar_attributes => {
1 => {
label: "gato"
},
2 => {
label: "perro"
}
}
}
}
但是,strong_params希望我將每個屬性明確列入白名單。
def foo_params
params.require(:foo).permit(:name, bar_attributes: {1: [:label], 2: [:label]}
end
:bar_attributes
的哈希數是動態的。 我是否需要動態生成foo_params
方法,特別是:bar_attributes
哈希? 還是有一種方法可以將我通常想要的參數列入白名單,而不是對:bar_attributes
每個子哈希進行白名單:bar_attributes
。
使用params.require(:foo).permit(:name, bar_attributes: [:label]}
應該可以正常工作,但bar_attributes
是bar_attributes
鍵是字符串(即“ 1”和“ 2”而不是1和2)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.