Post and redirect to external site from Rails controller?

做~自己de王妃 提交于 2019-11-30 09:24:49

问题


After the user registration page, web clients will come to this page, which contains only a proceed button. 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>

回答1:


You should do POST request from controller.

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.




回答2:


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. A common solution is to submit the form on load via javascript. e.g.

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



回答3:


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.

Some good examples for using net/http can be found here. https://github.com/augustl/net-http-cheat-sheet/blob/master/post_form.rb

Still that's probably not the best way to handle this.



来源:https://stackoverflow.com/questions/10614502/post-and-redirect-to-external-site-from-rails-controller

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