[], "B" => [], "C" => [], "D" => []} } 方法只看到{ -6ren">
gpt4 book ai didi

ruby-on-rails - Rails 5 params 对象具有空数组作为值被删除

转载 作者:行者123 更新时间:2023-12-04 07:50:42 25 4
gpt4 key购买 nike

发送如下所示的 Controller 参数时遇到问题:

{ id: "1", stuff: {"A" => [], "B" => [], "C" => [], "D" => []} }

方法只看到 { id: "1" }和整个 stuff参数被删除。

如果数组中有任何值,则可以更改此设置。但是假设所有数组中都有值,除了键 "C" ,除了 "C",他们都会在那里喜欢:
{ id: "1", stuff: {"A" => ["1"], "B" => ["2", "3"], "D" => ["4"]} }

我在从 Rails 4.2.x -> 5.0.0 升级时遇到了这个问题
关于这里发生的事情的任何建议?我在 munging parameters 周围看到了一些文章/问题,但我不确定这是否是问题所在,因为在他们关于 munging 工作原理的示例表中是 {person: []}变成 {person: nil} ,其中 person 参数没有完全删除。

最佳答案

来自 GH 社区的@sgrif:

This is the expected behavior. There is no way to encode an empty array using an HTML form (e.g. Content-Type: url-form-encoded). The reason your tests passed in Rails 4.2 is because controller tests did not encode their parameters, they simply passed the hash through directly. In Rails 5 it encodes them. If your controller cares about empty arrays, it's likely that you are dealing with JSON requests. You can do that in your test with as: :json. If you're just dealing with form input, you will never receive an empty array.



添加 as: :json最终没有为我工作,但添加 @request.headers["Content-Type"] = 'application/json'在测试开始时确实如此。

关于ruby-on-rails - Rails 5 params 对象具有空数组作为值被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40870882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com