Ruby-on-rails 在 rails 中使用 POST 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/985596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
redirect_to using POST in rails
提问by andi
Is it possible to redirect using a POST method ?
Or should redirects always be made using GET ?
是否可以使用 POST 方法重定向?
还是应该始终使用 GET 进行重定向?
The use for this is in the final steps of an order process for an e-commerce site, to send the data to the payment processor, without introducing an extra step for the user.
其用途是在电子商务网站的订购流程的最后步骤中,将数据发送到支付处理器,而无需为用户引入额外的步骤。
回答by Codebeef
Redirection isn't possible with POST requests - it's part of the HTTP/1.1 protocol.
POST 请求无法重定向 - 它是HTTP/1.1 协议的一部分。
You could either introduce another step that contains the form data to be POSTed to the payment processor, or you could send the post from your application (something I have done when working with PROTX).
您可以引入另一个步骤,其中包含要发布到支付处理器的表单数据,或者您可以从您的应用程序发送帖子(我在使用 PROTX 时所做的事情)。
回答by Mark Embling
I "solved" the issue by displaying a summary page with all the products and delivery charges etc, with the typical "to confirm and pay for your purchases, click the continue button below" type message. The continue button causes the site to POST the product data and everything across to the payment processor.
我通过显示包含所有产品和运费等的摘要页面“解决”了这个问题,典型的“确认并支付您的购买,点击下面的继续按钮”类型的消息。继续按钮使站点将产品数据和所有内容发布到支付处理器。
The short answer - there isanother step for the user. However, the key is to make it seem as natural and as much part of the checkout experience as you can. This way, it doesn't come across too much as "just another step".
简短的回答-有是为用户的又一步骤。然而,关键是让它看起来尽可能自然,并尽可能多地成为结账体验的一部分。这样,它不会被认为是“只是另一个步骤”。
However, if you docome across a better way, I'll be very interested to hear what it was :)
但是,如果您确实遇到了更好的方法,我会很想听听它是什么:)
回答by Alsciende
With a simple line of javascript, you can have your POST form to post itself (form.submit()). You can then hide the form and display a simple "please wait while..." message to the user while the form is submitted to the payment processor.
使用简单的一行 javascript,您可以让您的 POST 表单自行发布(form.submit())。然后,您可以隐藏该表单,并在将表单提交给支付处理器时向用户显示一条简单的“请稍候...”消息。
回答by yaro
The idea is to make a 'redirect' while under the hood you generate a form with method :post.
这个想法是在使用方法:post 生成表单时进行“重定向”。
I have faced the same problem and extracted the solution into the gem repost, so it is doing all that work for you, so no need to create a separate view with the form, just use the provided by gem function redirect_post()on your controller.
我遇到了同样的问题并将解决方案提取到 gem 中repost,因此它为您完成所有工作,因此无需使用表单创建单独的视图,只需redirect_post()在控制器上使用 gem 函数提供的功能。
class MyController < ActionController::Base
...
def some_action
redirect_post('url', params: {}, options: {})
end
...
end
回答by Abel
html hack: instead redirect_to, render this html template (based on Alsciende answer)
html hack:代替redirect_to,呈现这个html模板(基于Alsciende的回答)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<form id="step" name="step" action="<%= URL %>" method="POST">
<!-- optional params -->
<input type='hidden' name='token' value='e7295d6d1cd512a8621f1de5965b90a' />
</form>
<script type="text/javascript">
$(document).ready(function () {
setTimeout(function () {
$("#step").submit();
}, 0);
});
</script>

