views:

705

answers:

3

Hi,

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?

+1  A: 

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.

Frank Schmitt
This solution assumes that SiteA's routes.rb has a route that matches that controller/action pair. This isn't a problem if you've kept the default routes.
EmFi
+4  A: 

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.

Denis Hennessy
This solution assumes that SiteA's routes.rb has a route that matches that controller/action pair. This isn't a problem if you've kept the default routes.
EmFi
+1  A: 

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.

EmFi