In Rails 3, params
and request.parameters
refer to the same object.
With the addition of strong parameters in Rails 4, params
now refers to a distinct instance of ActionController::Parameters
that is created from request.parameters
.
A side effect of this is that once you have called params
(thereby creating the distinct Parameters
object), calling request.format=
will not update params
.
Rails 3:
params # set @_params to request.parameters
request.format = "mobile"
params[:format]
=> "mobile"
Rails 4:
params # set @_params to Parameters.new(request.parameters)
request.format = "mobile"
params[:format]
=> nil
This isn't technically a bug because it's easy enough for client code to look to request.format
instead of params[:format]
as the source of truth for this information (and not expect to be able to use both interchangeably).
But it feels like a design regression to me. Having params
and request.parameters
be the same "except for the exceptions" is causing a bug for us now, and I expect it to cause bugs for many devs in the future.
Am I Doing It Wrong? If so, why isn't this really an issue and what should I be doing differently? Note that the use case that brought me down this path is the exact one from the docs for format=.
This is likely the case because the params
hash is intended to be business data for your application, whereas request.format
represents the HTTP Request response requests'. So what you do with the contents of params
would still be the same, but you would have the flexibility to change the request format without changing the business data.
Think of it as separation of concerns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With