简体   繁体   English

Rails的强参数:允许嵌套属性

[英]Rails strong parameters: permitting nested attributes

Say I have controller params with the following structure: 假设我具有以下结构的控制器params

{
  "foo" => {
    "id" => 123,
    "children" => {
      "0" => {
        "a" => "a"
      },
      "1" => {
        "b" => "b"
      }
    }
  }
}

How can I permit all the data explicitly? 如何明确允许所有数据? I don't want to permit arbitrary data at any point in the hierarchy. 我不想在层次结构的任何位置允许任意数据。

I had expected this work: 我曾期望这项工作:

params.require(:foo).permit(:id, children: { "0" => [:a], "1" => [:b] })

However, it returns: 但是,它返回:

{ "id" => 123, "children" => { "0" => {}, "1" => {} } }

How can I whitelist the permitted attributes for each child? 如何将每个孩子的允许属性列入白名单?

Try square brackets instead of braces: 尝试用方括号代替大括号:

params.require(:foo).permit(
  :id,
  children: [
    "0": [:a],
    "1": [:b]
  ]
)

Try this 尝试这个

params.require(:foo).permit(:id, :children => { :"0" => [:a], :"1" => [:b] }) params.require(:foo).permit(:id,:children => {:“ 0” => [:a],:“ 1” => [:b]})

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

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