Rails 4: setting request.format does not update parameters [: format]

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 separate instance of ActionController::Parameters , which is created from request.parameters .

A side effect of this is that after calling params (thereby creating a separate 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 is not a technical error, because it is easy for client code to look like request.format instead of params[:format] as a source of truth for this information (and not count on the possibility of being used interchangeably).

But for me it is like a design regression. The presence of params and request.parameters will be the same โ€œexcept forโ€, which will lead to an error for us now, and I expect this to cause errors for many developers in the future.

Am I doing it wrong? If so, why is this not a problem, and what should I do differently? Note that the use case that led me along this path is the exact one from the docs for the format = .

+4
source share
1 answer

This is probably due to the fact that the params hash is intended for business data for your application, whereas request.format represents HTTP request response requests. "So what you do with the params content will still be the same, but you have there will be flexibility in changing the format of the request without changing the business data.

Think of it as a separation of concerns.

0
source

Source: https://habr.com/ru/post/1482065/


All Articles