redirect_to :overwrite_params => {:controller=>’survey’, :action=>’handle_single_scan’}

Here’s a little trick I discovered recently.

I wanted to rewrite a url, forwarding a request to another controller, with all the other parameters intact. Rather than define each individual parameter, I found that rails has a built in API to preserve the existing URL with just a few fields specified.

For instance, say I had a request going to http://www.mskynet.com/foo/log?id=123&param=456, and I wanted to redirect to http://www.mskynet.com/bar/do_work?id=123&param=456, I could do the following:

redirect_to :overwrite_params => {:controller=>’bar’, :action=>’do_work’}

This syntax would let you overwrite any of the parameters that you want to change and keep everything else the same.

If I just wanted to overwrite the parameter ‘id’ but keep the controller and action, I could do:

redirect_to :overwrite_params => {:id=>’234′}

Check out the :overwrite_params option here: http://api.rubyonrails.org/classes/ActionController/Base.html#M000649