How do i redirect user to external url via post method? I know redirect_to
works only for get
requests. How do i achieve this?
Asked
Active
Viewed 3,554 times
0

Avdept
- 2,261
- 2
- 26
- 48
-
1I am curious. What is the reason for that? – Kai Mattern Mar 17 '15 at 15:58
-
@KaiMattern me too. Thats what i've been ordered to do :( – Avdept Mar 17 '15 at 17:58
-
possible duplicate of [redirect\_to using POST in rails](http://stackoverflow.com/questions/985596/redirect-to-using-post-in-rails) – Brad Werth Aug 13 '15 at 16:25
-
No. 6 years long distance. A lot changed. – Avdept Aug 13 '15 at 17:06
1 Answers
4
You cannot redirect a user to a POST
method. You can present a user with a link that the Rails JS will turn into a form/POST:
= link_to "Do it!", "http://other.site/path", method: :post
...or a form that triggers a POST
to another site (note that I've included an extra param in this) eg.
= form_tag "http://other.site/path", method: :post do
= hidden_field_tag :foo, "Bar"
= submit_tag "Do it!"
...or you can make the request yourself from the server side on your user's behalf (here I'm using the httparty gem), note that I'm including the same foo=Bar
parameters:
class YourController < ApplicationController
include HTTParty
def some_action
self.class.post "http://other.site/path", query: { foo: "Bar" }
end
end
A few options for you.

smathy
- 26,283
- 5
- 48
- 68