Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails redirect_to "www.somewebsite.com" with GET/POST parameters?

I'm trying to issue a redirect_to in one of my controllers to a fully qualified URL + I want to pass in some parameters

In the controller for site A I do:

redirect_to: "www.siteB.com/my_controller/my_action?my_parameter=123"

Is there a nicer way to do this in rails?

like image 977
LouisePr Avatar asked Jan 07 '10 00:01

LouisePr


3 Answers

If SiteB is running the same app (i.e. the routes are the same for that server), then you can build the redirect you describe with:

redirect_to :host => "www.siteB.com", 
            :controller => "my_controller", 
            :action => "my_action",  
            :my_parameter => 123

Notice that any keys not handled by url_for are automatically encoded as parameters.

like image 187
Denis Hennessy Avatar answered Nov 15 '22 11:11

Denis Hennessy


You can apparently pass a host:

redirect_to { :host => "www.siteB.com", :controller => "my_controller", :action => "my_action",  :id => 123 }

Check out the documentation for url_for.

like image 42
Frank Schmitt Avatar answered Nov 15 '22 12:11

Frank Schmitt


Along the lines of the other responses. If you set up a controller defining the path in your routes.rb of site A, you can use the generated url helpers. Just override the :host as an argument.

Example:

Site A Routes.rb:

...
map.resource whatever
...

Site A Controller:

...
redirect_to edit_whatever_url(:host => "www.siteB.com", :my_parameter => 123)
...

So long as SiteB's web server (rails or otherwise) recognizes the http://www.siteB.com/whaterver/edit?my_parameter=123 you're good.

Caveat: Keep in mind that redirecting a Post with 302 has specific consequences as defined in RFC 2616. In a nutshell it means that a user will be asked to reconfirm their post to the new URL, before the redirected post can succeed.

like image 43
EmFi Avatar answered Nov 15 '22 12:11

EmFi