Applying the Post Redirect Get Pattern to Striped Application


We will use the Striped project as an example to illustrate the Post-Redirect-Get pattern. Purchase a product as a guest. When the checkout form does a post to the sales controller create action, the app/views/sales/create.html.erb is displayed. If you refresh the browser when the sales/create page is displayed you will get a 'Confirm Form Resubmission' popup window. The customer payment should happen only once for a product, since the POST is not idempotent, we should redirect the user after the purchase is complete. This page will use a GET request to display the purchase confirmation page. Since GET is a safe operation, it will charge the customer more than once, the user can refresh the browser without affecting the state on the server.


Step 1

Define the route for the purchase confirmation path.

get 'sales/show', as: :purchase_confirmation

Step 2

Change the sales controller create action to redirect the user to the purchase confirmation page.

  def create    
    log_message = 'Guest checkout failed due to'
    user_message = 'Checkout failed. We have been notified about this problem'

    cleanup = -> { session[:product_id] = nil }
    main = -> do
      user = current_or_guest_user
      @payment = Actors::Customer::UseCases.guest_checkout(session[:product_id], params[:stripeToken], user)
      @product = Product.find(session[:product_id])      

    run_with_stripe_exception_handler(log_message, user_message, main, cleanup)

    redirect_to purchase_confirmation_path(product_id: @product, payment_id: @payment)

The last line of the create action now has the redirect.

Step 3

Rename the app/views/create.html.erb to app/views/show.html.erb.

Step 4

Define the show page that loads the product and payment models so that the details of purchase can be displayed.

  def show
    @product = Product.find(params[:product_id])
    @payment = Payment.find(params[:payment_id])

Now if you purchase a product, the redirect will make the purchase confirmation page safe to refresh. You can download the entire source code for this article using the commit hash 02a7246 from


Redirect After Post

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.