Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent user going back and viewing previously submitted form Rails

I have a payment page and when the user submits, it captures the payment and directs to a thank you page. The problem is that when the user clicks back, the browser takes them back to the previously submitted page with the payment form and all.

How can i prevent the user from accessing the previous page?

Thanks

like image 958
Robbo Avatar asked Oct 15 '25 15:10

Robbo


1 Answers

@James, put this method in your application controller and call this method on before_action callback like -

before_action :set_cache_buster

and then define the action in protected method like ->

protected

def set_cache_buster
  response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
  response.headers["Pragma"] = "no-cache"
  response.headers["Expires"] = "#{1.year.ago}"
end

To accomplish this we just need to disable browser caching using appropriate HTTP headers. Here’s the secret:

Cache-Control: no-cache, max-age=0, must-revalidate, no-store

Taken individually, each of these Cache-Control attributes would seem to prevent caching. In practice, no-cache and no-store are usually interchangeable in most browsers. However for the back button cache specifically, Firefox will only disable this cache if no-store is specified. To play it safe and guarantee cross-browser compatibility, you should use all four attributes.

For more info see the link - Difference between Pragma and Cache-Control headers?

For specific page:

Add that callback on the specific page with the only option like:

before_action :set_cache_buster, only: [:your_action_name]
like image 56
Chitra Avatar answered Oct 18 '25 03:10

Chitra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!