Prevent user going back and viewing previously sub

2020-06-29 02:09发布

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

1条回答
家丑人穷心不美
2楼-- · 2020-06-29 02:59

@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?

Hope you enjoy this.

For specific page ->

1) Add that callback on specific page with only option like ->

before_action :set_cache_buster, only: [:your_action_name]

查看更多
登录 后发表回答