Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 具有嵌套哈希的强参数_Ruby On Rails_Ruby_Strong Parameters - Fatal编程技术网

Ruby on rails 具有嵌套哈希的强参数

Ruby on rails 具有嵌套哈希的强参数,ruby-on-rails,ruby,strong-parameters,Ruby On Rails,Ruby,Strong Parameters,我有以下参数,无法使用强参数 以下是我的基本代码,为简单起见,可在Rails控制台中运行: json = { id: 1, answers_attributes: { c1: { id: "", content: "Hi" }, c2: { id: "", content: "Ho" } } } params = ActionController::Parameters.new(json) 我读过的所有内容都说下面的内容应该有效,但它只给了我id和answers\

我有以下参数,无法使用强参数

以下是我的基本代码,为简单起见,可在Rails控制台中运行:

json = {
  id: 1,
  answers_attributes: {
    c1: { id: "", content: "Hi" },
    c2: { id: "", content: "Ho" }
  }
}

params = ActionController::Parameters.new(json)
我读过的所有内容都说下面的内容应该有效,但它只给了我
id
answers\u属性的空散列:

params.permit(:id, answers_attributes: [:id, :content])
=> { "id"=>1, "answers_attributes"=>{} }

如果我改为手动列出
c1
c2
(如下所示),它会起作用,但这真的很愚蠢,因为我不知道用户将提供多少答案,这是一个很大的重复:

params.permit(:id, answers_attributes: { c1: [:id, :content], c2: [:id, :content] })
=> {"id"=>1, "answers_attributes"=>{"c1"=>{"id"=>"", "content"=>"Hi"}, "c2"=>{"id"=>"", "content"=>"Ho"}}}

我已尝试将
c1
c2
替换为
0
1
,但我仍然必须在许可证声明中手动提供
0
1


如何允许嵌套属性的未知长度数组?

使用以下语法完成:

answers_attributes: [:id, :content]
问题在于您在
答案\u属性中使用的键。它们应该是
answers\u属性的ID
,如果是新记录
0

更改这些选项可以获得预期的结果:

json = {
  id: 1,
  answers_attributes: {
    "1": { id: "", content: "Hi" },
    "2": { id: "", content: "Ho" }
  }
}

params = ActionController::Parameters.new(json)

params.permit(:id, answers_attributes:[:id, :content])
=>  {"id"=>1, "answers_attributes"=>{"1"=>{"id"=>"", "content"=>"Hi"}, "2"=>{"id"=>"", "content"=>"Ho"}}}

编辑:似乎0不是唯一的键,我的意思是如果你有两条新的记录。我使用,它似乎使用了一个很长的随机数。

您的
answers\u属性包含c1和c2,这是不允许的

方式1:您可以将嵌套属性作为哈希数组传递

json = {
  id: 1,
  answers_attributes: [ { id: "", content: "Hi" }, { id: "", content: "Ho" } ]
}
现在
params.permit(:id,answers\u属性:[:id,:content])
给出

{"id"=>1, "answers_attributes"=>[{"id"=>"", "content"=>"Hi"}, {"id"=>"", "content"=>"Ho"}]}
方式2:您可以将其作为散列进行传递,如

json = {
  id: 1,
  answers_attributes: {
    c1: { id: "", content: "Hi" },
    c2: { id: "", content: "Ho" }
  }
}

方式1方式2在模型水平上具有相同的效果。但是
permit
不允许传递值,除非明确指定了键。因此,除非明确规定,否则不允许使用
c1
c2

params.permit(:id, answers_attributes: [c1: [:id, :content], c2: [:id, :content]])

这真是让人头疼。

如果是新记录,你会特别说
”。这也适用于已编辑的记录吗?已编辑的记录不是新记录,因此您可以使用
id
作为键。谢谢,这确实起到了作用。奇怪的是,它必须是一个数字的字符串表示,而不是一个实际的数字,但我可以让它工作。谢谢你的帮助@我不认为它们必须是弦。我将编辑答案。
c1
c2
对于
接受嵌套属性确实有效,因为这一直有效,并且在我开始使用
permit
之前保存了数据。问题是
permit
调用不理解此语法,并且没有将其传递给要保存的模型。编辑:请注意,如果您使用JSON,您上面的方式2也不适用于许可证。谢谢通知。正如@TopherFangio提到的,除非我们明确指定了键,否则permit不允许传递值。我将编辑我的答案。