Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge ActionController::Parameters with rails 5

In rails 4, I can merge! StrongParams, but since rails 5 (beta1) merge! is not available. Which is the best way to do that in a controller

  params = ActionController::Parameters.new({
             name: 'Francesco',
             age:  22,
             role: 'admin'
         })
         params.merge!(city: "Los Angeles")
like image 407
Olivier Avatar asked Sep 18 '25 23:09

Olivier


2 Answers

As far as I can see from the source code, you have merge not merge!. In other words, it doesn't seem to be possible to modify the hash in place.

The following code will work:

params = ActionController::Parameters.new({
             name: 'Francesco',
             age:  22,
             role: 'admin'
         })
params = params.merge(city: "Los Angeles")
like image 145
Simone Carletti Avatar answered Sep 22 '25 16:09

Simone Carletti


params.merge!(city: "Los Angeles") works with Rails5.0.1

enter image description here

In Rails 5: ActionController::Parameters Now Returns an Object Instead of a Hash.

so you must use params.permit(:city).to_h to access city.

For more details how ActionController::Parameters works in Rails5?

Ref: http://www.rortuts.com/ruby-on-rails/rails5-actioncontrollerparameters/

like image 44
Vaibhav Avatar answered Sep 22 '25 14:09

Vaibhav