redirect_to using POST in rails

Ruby on-RailsRubyRedirect

Ruby on-Rails Problem Overview


Is it possible to redirect using a POST method ?
Or should redirects always be made using 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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Redirection isn't possible with POST requests - it's part of the HTTP/1.1 protocol.

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).

Solution 2 - Ruby on-Rails

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 is another 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 do come across a better way, I'll be very interested to hear what it was :)

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

The idea is to make a 'redirect' while under the hood you generate a form with method :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.

class MyController < ActionController::Base
...
  def some_action
    redirect_post('url', params: {}, options: {})
  end
...
end

Solution 5 - Ruby on-Rails

html hack: instead 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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandiView Question on Stackoverflow
Solution 1 - Ruby on-RailsCodebeefView Answer on Stackoverflow
Solution 2 - Ruby on-RailsMark EmblingView Answer on Stackoverflow
Solution 3 - Ruby on-RailsAlsciendeView Answer on Stackoverflow
Solution 4 - Ruby on-RailsyaroView Answer on Stackoverflow
Solution 5 - Ruby on-RailsAbelView Answer on Stackoverflow