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

时间:2018-06-20 21:14:02

标签: ruby-on-rails strong-parameters

说我有一个具有以下结构的控制器params

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

如何明确允许所有数据?我不想在层次结构的任何位置允许任意数据。

我曾期望这项工作:

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

但是,它返回:

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

如何将每个孩子的允许属性列入白名单?

2 个答案:

答案 0 :(得分:0)

尝试用方括号代替大括号:

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

答案 1 :(得分:0)

尝试

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