问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/29103568/redirect-user-to-external-url-with-post-method-and-params