加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:12:06 所属栏目:百科 来源:网络整理
导读:我发送一个看起来像这样的控制器参数时遇到问题: { id: "1",stuff: {"A" = [],"B" = [],"C" = [],"D" = []} } 该方法只看到{id:“1”},并删除整个stuff参数. 如果数组中有任何值,则可以更改此值.但是说除了键“C”之外所有数组都有值,除了“C”之外它们都
我发送一个看起来像这样的控制器参数时遇到问题:
{ 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’.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读