Why was :overwrite_params deprecated in Rails 3
With overwrite_params, I was doing this to prepare a PDF re开发者_JS百科quest for a page:
url_for(:overwrite_params => {:format => :pdf})
overwrite_params has been deprecated after Rails 2.3.8, is there a reason for this deprecation? What's the standard accepted alternative?
This is what your looking for:
url_for params.merge(:format => "PDF", :only_path => false)
This will create an absolute url for the current page including the current params.
Note that the suggested solution of merging with params
is not correct as it also contains data from the request body (sometimes misleadingly referred to as POST variables). A better (the correct?) solution is to merge with request.GET
instead (another misnomer as e.g. POST requests obviously may contain query parameters, too).
Using overwrite_params is allowing you to make mistakes and bad design - you are not protected from overwriting params in urls which are not relevant to your call.
It is much better aways know what is coming. Keep your params in a bean/model and serialize this bean to url.
If you want to break the rules of the good design you can aways do it yourself(with the .merge option) but then it is your responsibility.
I am guessing they deprecated the method to simplify it, so you just have to pass in the parameters you want to overwrite, instead of writing :overwrite_params. It makes sense that this method should overwrite the parameters by default.. if you are specifying parameters you obviously want to use the specified values instead of the existing.
You should be able to do it like this instead:
url_for(:format => :pdf)
A quick test on my users index page returns this:
/users.pdf
精彩评论