【问题标题】:Redirect user to external url with post method and params使用 post 方法和参数将用户重定向到外部 url
【发布时间】:2015-03-17 15:45:48
【问题描述】:

如何通过 post 方法将用户重定向到外部 url?我知道redirect_to 仅适用于get 请求。我如何做到这一点?

【问题讨论】:

  • 我很好奇。这是什么原因?
  • @KaiMattern 我也是。这就是我被命令做的:(
  • redirect_to using POST in rails 的可能重复项
  • 没有。 6年的长途。变化很大。

标签: ruby-on-rails redirect http-post httparty


【解决方案1】:

您不能将用户重定向到POST 方法。您可以向用户展示 Rails JS 将转换为表单/POST 的链接:

= link_to "Do it!", "http://other.site/path", method: :post

...或触发POST 到另一个站点的表单(请注意,我在其中包含了一个额外的参数)例如。

= form_tag "http://other.site/path", method: :post do
  = hidden_field_tag :foo, "Bar"
  = submit_tag "Do it!"

...或者您可以代表您的用户从服务器端自己发出请求(这里我使用httparty gem),请注意我包含相同的foo=Bar 参数:

class YourController < ApplicationController
  include HTTParty

  def some_action
    self.class.post "http://other.site/path", query: { foo: "Bar" }
  end
end

为您提供一些选择。

【讨论】:

  • 也结束了 Httparty 解决方案
猜你喜欢
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 2021-07-23
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多