marcamillion marcamillion - 20 days ago 5
Ruby Question

How do I modify the `params` that were set in the controller in an action?

I have the following parameters being passed to my

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>


What I want to do is delete the hash with key
1479585381276
(or any hash for that matter)

I tried using
.delete(key)
and that doesn't seem to work.

> 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>


The reason I want to do this is because when I evaluate the parameters, it still gets passed to the
@profile.update(profile_params)
in the latter half of my
Profiles#Update
and creates a record I don't want it to create.

So all I want to do is upon successful processing, delete it/pop it/remove it from the
profile_params[:videos_attributes]
hash.

Answer

profile_params returns you a new hash each time (with copy of whitelisted data from params). Modify params directly.

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

I've been burnt by this before. :)