简体   繁体   English

从 Rails 控制器发布并重定向到外部站点?

[英]Post and redirect to external site from Rails controller?

After the user registration page, web clients will come to this page, which contains only a proceed button.在用户注册页面之后,Web 客户端将来到此页面,其中仅包含一个继续按钮。 But I want to skip this page and proceed to the payment page.但我想跳过这个页面并进入支付页面。 Is there any way to do this action completely in the controller?有没有办法在控制器中完全执行此操作?

<form method="post" action="https://www.ccavenue.com/shopzone/cc_details.jsp">
    <input type=hidden name=Merchant_Id value="<%= @Merchant_id %>">
    <input type=hidden name=Amount value="<%= @Amount %>">
    <input type=hidden name=Order_Id value="<%= @user_id %>">
    <input type=hidden name=Redirect_Url value="<%= @redirect_url %>">
    <input type=hidden name=Checksum value="<%= @checksum %>">
    <input type="submit" class="Register_button" value="Proceed to payment">
</form>

You should do POST request from controller.您应该从控制器执行POST请求。

uri = URI.parse("https://www.ccavenue.com/shopzone/cc_details.jsp")
http = Net::HTTP.new(uri.host, uri.port)

form_data = {
  "Merchant_Id" => @Merchant_id,
  "Amount" => @Amount,
  "Order_Id" => @user_id,
  "Redirect_Url" => @redirect_url,
  "Checksum" => @checksum
}

request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(form_data)

response = http.request(request)

You can use httparty gem to do post request.您可以使用httparty gem 进行发布请求。

No, not the way you're wanting.不,不是你想要的方式。

As others have said, you can POST from within your controller, and receive the response, but if you want to have the browser post to the remote page, it has to be through a form.正如其他人所说,您可以从控制器内 POST 并接收响应,但如果您想让浏览器发布到远程页面,则必须通过表单。 A common solution is to submit the form on load via javascript.一个常见的解决方案是通过 javascript 在加载时提交表单。 eg例如

<body onload="document.forms['myform'].submit();">
    <form id="myform">
        …
    </form>
</body>

This might be years late response, but in case someone is still looking, I found this Repost plugin that could achieve this behaviour.这可能是几年后的响应,但如果有人仍在寻找,我发现这个Repost 插件可以实现这种行为。

Could be used just like the normal redirect_to method in controller.可以像控制器中的普通redirect_to方法一样使用。

redirect_post('https://external-url.io/endpoint', params: {...})

There's no reason you couldn't post from a controller using uri and net/http in the Ruby standard library and handle the response from the server there.您没有理由不能使用 Ruby 标准库中的 uri 和 net/http 从控制器发布信息并处理来自那里的服务器的响应。

Some good examples for using net/http can be found here.可以在这里找到一些使用 net/http 的好例子。 https://github.com/augustl/net-http-cheat-sheet/blob/master/post_form.rb https://github.com/augustl/net-http-cheat-sheet/blob/master/post_form.rb

Still that's probably not the best way to handle this.尽管如此,这可能不是处理此问题的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM