如何修改在动作中控制器中设置的“params”?
How do I modify the `params` that were set in the controller in an action?
我将以下参数传递给我的 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>
我之所以要这样做是因为当我评估参数时,它仍然会在我的 Profiles#Update
的后半部分传递给 @profile.update(profile_params)
并创建一个我没有创建的记录想要它创建。
所以我想做的就是在成功处理后,从 profile_params[:videos_attributes]
哈希中删除 it/pop it/remove 它。
profile_params
returns 每次你一个新的散列(从 params
复制白名单数据)。直接修改params
。
params[:profile][:videos_attributes].delete(item)
我以前被这个烧过。 :)
我将以下参数传递给我的 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>
我之所以要这样做是因为当我评估参数时,它仍然会在我的 Profiles#Update
的后半部分传递给 @profile.update(profile_params)
并创建一个我没有创建的记录想要它创建。
所以我想做的就是在成功处理后,从 profile_params[:videos_attributes]
哈希中删除 it/pop it/remove 它。
profile_params
returns 每次你一个新的散列(从 params
复制白名单数据)。直接修改params
。
params[:profile][:videos_attributes].delete(item)
我以前被这个烧过。 :)