Rails 5 params,对象具有空数组,因为值被删除

时间:2022-02-27 00:16:55

I'm having a problem when sending a controller params that look like this:

我发送一个看起来像这样的控制器参数时遇到问题:

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

The method only sees { id: "1" } and the entire stuff parameter is dropped.

该方法只看到{id:“1”},并删除整个stuff参数。

This can be changed if there are any values in the arrays. But say there are values in all the arrays except for the key "C", they will all be there besides "C" like:

如果数组中有任何值,则可以更改此值。但是说除了键“C”之外所有数组都有值,除了“C”之外它们都会在那里:

{ id: "1", stuff: {"A" => ["1"], "B" => ["2", "3"], "D" => ["4"]} }

I'm falling into this problem upgrading from Rails 4.2.x -> 5.0.0 Any suggestions on what is happening here? I've seen a few articles/issues around munging parameters, but I'm not sure if that's the issue because in their example table of how munging works is {person: []} becomes {person: nil}, where the person param isn't dropped completely.

我正在从Rails 4.2.x升级到这个问题 - > 5.0.0这里有什么建议吗?我已经看到了一些关于修改参数的文章/问题,但是我不确定这是不是问题,因为在他们的示例表中,如何修改工作是{person:[]}变成{person:nil},其中人员参数没有完全掉下来。

1 个解决方案

#1


6  

From @sgrif in the GH community:

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

这是预期的行为。无法使用HTML表单对空数组进行编码(例如,Content-Type:url-form-encoded)。你在Rails 4.2中传递测试的原因是因为控制器测试没有对它们的参数进行编码,它们只是直接传递了哈希值。在Rails 5中,它对它们进行编码。如果您的控制器关心空数组,那么您可能正在处理JSON请求。您可以使用as :: json在测试中执行此操作。如果您只处理表单输入,则永远不会收到空数组。

Adding as: :json didn't end up working for me, but adding @request.headers["Content-Type"] = 'application/json' at the beginning of the test did.

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

#1


6  

From @sgrif in the GH community:

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

这是预期的行为。无法使用HTML表单对空数组进行编码(例如,Content-Type:url-form-encoded)。你在Rails 4.2中传递测试的原因是因为控制器测试没有对它们的参数进行编码,它们只是直接传递了哈希值。在Rails 5中,它对它们进行编码。如果您的控制器关心空数组,那么您可能正在处理JSON请求。您可以使用as :: json在测试中执行此操作。如果您只处理表单输入,则永远不会收到空数组。

Adding as: :json didn't end up working for me, but adding @request.headers["Content-Type"] = 'application/json' at the beginning of the test did.

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