[ {"id"=>"1", "children"=>[{"id"=>"2"}, -6ren">
gpt4 book ai didi

ruby-on-rails - 允许多维参数

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:42 24 4
gpt4 key购买 nike

我收到这个参数:

<ActionController::Parameters 
{"page_hierarchy"=>
[<ActionController::Parameters
{"id"=>"6", "children"=>[
{"id"=>"1", "children"=>[{"id"=>"2"},
{"id"=>"5"},
{"id"=>"3"},
{"id"=>"18"},
{"id"=>"4"}]}]} permitted: false>]} permitted: false>

页面 "page_hierarchy" 是一个由多个 "{id: , children: }" 元素组成的数组。children 也是一个由多个 "{id: , children: }" 元素组成的数组。

我如何允许这种格式的参数?

像这样的解决方案对我不起作用,因为数组是多维的 Permit array in params

最佳答案

您可以通过允许散列而不是数组来做到这一点,如下所示:

params.permit(page_hierarchy: {})

Rails Guides进一步详细解释:

Sometimes it is not possible or convenient to declare the valid keys of a hash parameter or its internal structure. Just map to an empty hash:

params.permit(preferences: {}) but be careful because this opens the door to arbitrary input. In this case, permit ensures values in the returned structure are permitted scalars and filters out anything else.

由于您的参数嵌套很深,您可能必须将整个哈希列入白名单,您可以使用 permit! 来做到这一点,但请注意这可能会引入安全问题:

params.permit!(:page_hierarchy)

同样,取自 Rails Guides :

To whitelist an entire hash of parameters, the permit! method can be used:

params.require(:log_entry).permit! This marks the :log_entry parameters hash and any sub-hash of it as permitted and does not check for permitted scalars, anything is accepted. Extreme care should be taken when using permit!, as it will allow all current and future model attributes to be mass-assigned.

关于ruby-on-rails - 允许多维参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42346779/

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