微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我发送一个看起来像这样的控制器参数时遇到问题:
{ 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左右看过一些文章/问题,但是我不确定这是不是问题,因为在他们的示例表中,如何修改工作是{person:[]}变成{person:nil},其中人员param isn完全掉了下来.

解决方法

来自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.

添加为:: json最终没有为我工作,但在测试开始时添加@ request.headers [“Content-Type”] =’application / json’.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐