0

If a user reaches the ticket book page (book.html.erb)

Case 1: Browser's Back button should not work here. The user will remain to stay on the same page book ticket.

Case 2: If the user clicks Browser's Back button many times in the case also user will remain to stay on the same page book ticket.

Case 3: If the User entered any URL in Browser in this case user remains to stay on the same page book ticket.

Please suggest any rails functionality to achieve this or suggest any JavaScript for this.

inmydelorean
  • 448
  • 6
  • 13
  • You can't do this in ruby, it's a client-side concern - i.e. JavaScript. There are various [hacks/workarounds](https://stackoverflow.com/q/12381563/1954610) you could [try](https://www.aspsnippets.com/Articles/Disable-Browser-Back-Button-Functionality-using-JavaScript.aspx). Have you written any code? – Tom Lord Mar 05 '21 at 14:44
  • It would be most common / best practice to simply **WARN** the user when they're navigating away from your page, not try to lock them in by disabling all other functionality on their machine. What's next, are you going to ask how to disable the power button until they've given your their credit card details? ;) – Tom Lord Mar 05 '21 at 14:48

1 Answers1

0

You cannot prevent the user from leaving your page, there is only workarounds (as Tom Lord said) and none of them will, in fact, prohibit this action to users, it'll only help to make "more difficult" to leave your site. Clients don't have enough permissions to force a browser to behave this way.

Said that, in Rails, one workaround would be to use before_action on your ApplicationController, and redirect_to your ticket book page if some variable/attribute/status indicates that.

Using this you will be able to force the user to always access to the same url, only if he's on your domain.

Dharman
  • 30,962
  • 25
  • 85
  • 135
anonymus_rex
  • 470
  • 4
  • 18