如何修改操作中在控制器中设置的`params`?
问题描述:
我已经被传递给我的ProfilesController#Update
以下参数:如何修改操作中在控制器中设置的`params`?
> 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)
我以前被烧过了。 :)
这是100%的钱。非常感谢! – marcamillion