This may be more a ruby question then rails question but I'm pretty sure I was able to do this in a vanilla ruby application.
I have strong params defined.
def trip_params
params.require(:trip).permit(:name, :date)
end
Now I get those params in a controller method. I want to do this.
def save
trip_params[:name] = 'Modifying name in place'
#trip_params[:name] still equals original value passed
end
This never works. Name never changes. BTW: The type of trip_params is ActionController::Parameters
If I do a standard ruby script, it works.
test = {}
test[:name] = "blah"
test[:name] = "ok"
puts test #{:name=>"ok"}
This is because there's no method such as
trip_params[]=(arg, val).
I mean, when you call trip_params you are returning the value of
params.require(:trip).permit(:name, :date)
, so every time you calltrip_params
you are getting the params again.So, if I were you, I'd define the trip_params method as follow:
And would also define a method to change trip_params
So now when you call
trip_params
you would actually return@trip_params
, and if@trip_params
is not set yet it would set toparams.require(:trip).permit(:name, :date)
And then when you call
trip_params[:name] = "Some name"
it will ensure first that@trip_params
is initialized by callingtrip_params and then it will set the :name param to
"Some name"`Hope I've helped you
If you really want to change params in controller you can do it on this way:
permit
returns a new hash with those keys in it, so you're not modifying the realparams
variable. You're also not saving a reference to the hash trip_params returns, so you get it fresh each call insave
.Try this:
Or, if you really want it to be used the way you previously did, modify
trip_params
like so:Now that hash is lazily cached and the same one is returned on subsequent
trip_params
calls.You could also do
If a new hash is merged into an old hash and if there are duplicate keys, the new hash's keys overwrite the old hash's matching keys.