Redirect_to using POST in rails

Is it possible to redirect using the POST method?

Or do you always need to redirect using GET?

The use for this consists in the final stages of the ordering process for an electronic commerce site, for sending data to the payment processor without introducing an additional step for the user.

+57
redirect ruby ruby-on-rails
Jun 12 '09 at 8:29
source share
5 answers

Redirection is not possible with POST requests - this is part of the HTTP / 1.1 protocol .

You can enter another step that contains the form data that must be sent to the payment processor, or you can send a message from your application (which I did when working with PROTX).

+76
Jun 12 '09 at 8:30
source share

I "solved" the problem by indicating a summary page with all the products and delivery costs, etc., with a typical "confirmation and payment of purchases", click the "Continue" button below the "Message Type". The "Continue" button causes the POST website to transfer product data and everything to the payment processor.

The short answer is that there is one more step for the user. However, the key is to make it seem natural and as much of the testing experience as possible. So it doesn’t mean too much, "just one more step."

However, if you come across a better way, I will be very interested to know what it is :)

+14
Jun 12 '09 at 9:02
source share

With a simple javascript line, you can have your POST form for publishing (form.submit ()). Then you can hide the form and display the message "please wait while ..." to the user while the form is submitted to the payment processor.

+11
Jun 12 '09 at 9:47
source share

The idea is to do a "redirect" while you create the form using the: post method.

I ran into the same problem and extracted the solution in repost gem, so it does all the work for you, so there is no need to create a separate view with the form, just use the redirect_post() function provided by the gem function on your controller,

 class MyController < ActionController::Base ... def some_action redirect_post('url', params: {}, options: {}) end ... end 
0
Jan 25 '19 at 12:19
source share

html hack: instead of redirect_to render this html template (based on Alsciende answer)

 <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> 
0
Mar 21 '19 at 22:31
source share



All Articles