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

前端之家收集整理的这篇文章主要介绍了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’.

原文链接:https://www.f2er.com/ruby/270296.html

猜你在找的Ruby相关文章