gpt4 book ai didi

ruby-on-rails - 如何修改在 Action 中 Controller 中设置的 `params`?

转载 作者:数据小太阳 更新时间:2023-10-29 08:43:32 24 4
gpt4 key购买 nike

我将以下参数传递到我的 ProfilesController#Update:

> profile_params[:videos_attributes]
=> <ActionController::Parameters {"1479585381276"=><ActionController::Parameters {"vimeo_url"=>"https://www.youtube.com/watch?v=Qj2gkjh3-k", "official"=>"false", "_destroy"=>"false"} permitted: true>, "1479585385202"=><ActionController::Parameters {"vimeo_url"=>"https://vimeo.com/some-awesome-video", "official"=>"true", "_destroy"=>"false"} permitted: true>} permitted: true>

我想做的是删除键为 1479585381276 的散列(或与此相关的任何散列)

我尝试使用 .delete(key) 但这似乎不起作用。

> item
=> "1479585381276"
> profile_params[:videos_attributes].delete(item)
=> <ActionController::Parameters {"vimeo_url"=>"https://www.youtube.com/watch?v=Qas34Pce-k", "official"=>"false", "_destroy"=>"false"} permitted: true>
> profile_params[:videos_attributes]
=> <ActionController::Parameters {"1479585381276"=><ActionController::Parameters {"vimeo_url"=>"https://www.youtube.com/watch?v=Qas34Pce-k", "official"=>"false", "_destroy"=>"false"} permitted: true>, "1479585385202"=><ActionController::Parameters {"vimeo_url"=>"https://vimeo.com/some-awesome-video", "official"=>"true", "_destroy"=>"false"} permitted: true>} permitted: true>

我想这样做的原因是因为当我评估参数时,它仍然被传递到 @profile.update(profile_params) 在我的 Profiles#Update 的后半部分 并创建一条我不希望它创建的记录。

所以我想做的就是在成功处理后,删除它/弹出它/从 profile_params[:videos_attributes] 哈希中删除它。

最佳答案

profile_params 每次都会为您返回一个新的哈希值(包含来自 params 的白名单数据的副本)。直接修改params

params[:profile][:videos_attributes].delete(item)

我以前被这个烧过。 :)

关于ruby-on-rails - 如何修改在 Action 中 Controller 中设置的 `params`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40697429/

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