Redirect user to external url with post method and params

时光毁灭记忆、已成空白 提交于 2019-12-11 20:16:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!