具有嵌套哈希的强参数(Strong parameters with nested hash)

编程入门 行业动态 更新时间:2024-10-28 20:31:10
具有嵌套哈希的强参数(Strong parameters with nested hash)

我有以下参数,无法获得强大的参数。

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

json = { id: 1, answers_attributes: { c1: { id: "", content: "Hi" }, c2: { id: "", content: "Ho" } } } params = ActionController::Parameters.new(json)

我读过的所有内容都说明以下内容应该有效,但它只给我一个id和一个空的answers_attributes哈希:

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"}}}

我尝试用0和1替换c1和c2 ,但我仍然需要在permit语句中手动提供0和1 。

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

I have the following params and cannot get the strong parameters to work.

Here's my basic code, runnable in the Rails console for simplicity:

json = { id: 1, answers_attributes: { c1: { id: "", content: "Hi" }, c2: { id: "", content: "Ho" } } } params = ActionController::Parameters.new(json)

Everything I've read says the following should work, but it only gives me the id and an empty hash of answers_attributes:

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

If I instead manually list c1 and c2 (like below) it works, but this is really stupid because I don't know how many answers the user will supply, and this is a lot of duplication:

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

I've tried replacing c1 and c2 with 0 and 1, but I still have to manually supply the 0 and 1 in my permit statement.

How can I permit an unknown length array of nested attributes?

最满意答案

它的语法如下:

answers_attributes: [:id, :content]

问题是您在answers_attributes中使用的answers_attributes 。 它们应该是answers_attributes的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不是唯一的键,我的意思是如果你有两个new记录。 我使用nested_form ,它似乎使用一个很长的随机数。

It's done with syntax like:

answers_attributes: [:id, :content]

The problem is the keys you are using in the answers_attributes. They are expected to be the ids of the answers_attributes or in the case of new records 0.

Changing these gives your expected outcome:

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"}}}

Edit: It appears that 0 is not the only key, I mean what if you have two new records. I use nested_form and it appears to use a very long random number.

更多推荐

本文发布于:2023-04-28 04:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329865.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   参数   Strong   hash   nested

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!