Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails' routes: how to pass "all" request params in a redirect

I want to redirect all root requests to a /pages/home url, but I want to keep all the params used in the original request.

So:

http://myserver.com/?param1=value1&param2=value2 

Becomes

http://myserver.com/pages/home?param1=value1&param2=value2 

There are several SO questions about passing params in a redirect but I haven't found any related to passing request's params.

like image 200
fguillen Avatar asked Jun 05 '13 14:06

fguillen


2 Answers

# routes.rb root :to => redirect { |params, request| "/pages/home?#{request.params.to_query}" } 

Update 1

You can also play with the request.params to build the new path:

root :to => redirect { |params, request| "/pages/#{request.params[:page]}.html?#{request.params.to_query}" } 
like image 131
fguillen Avatar answered Sep 28 '22 16:09

fguillen


The other answer works but leaves a ? at the end in case there are no parameters.

This seems to do the trick without the side effects:

root to: redirect(path: '/quick_searches/new') 

Hope it helps!

like image 38
Kaukas Avatar answered Sep 28 '22 16:09

Kaukas