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?
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With