1

I am following Michael Hartl's Tutorial.

When a user signs in, then signs out and clicks on browser's back button, then the user's home page is displayed, which I don't want. I want the user to get the sign in page when he clicks on browser's "Back" button and not the homepage after a sign-out.

Thanx! :)

Vatsal Singh
  • 153
  • 1
  • 2
  • 9

2 Answers2

2

Basically your browser is caching the the web pages, and rails is currently configured to allow you to do that. You will have to manually specify in rails that you do not want page caching to occur.

Refer to this link here. It should be exactly what you are looking for.

Basically what the page I linked is telling you to do is to add a couple of lines to application_controller.rb:

This:

before_filter :set_no_cache

and the function:

def set_no_cache
response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end

Let me know if it works or not.

Danish M.
  • 997
  • 1
  • 10
  • 19
  • Hey, I tried this code already. It's given here http://stackoverflow.com/questions/711418/how-to-prevent-browser-page-caching-in-rails I guess, I am doing something wrong coz it worked for everyone. Is there anything I need to do after adding this code in application_controller.rb? – Vatsal Singh Jul 05 '12 at 06:43
  • This is how my application_controller.rb looks like `class ApplicationController < ActionController::Base protect_from_forgery include SessionsHelper before_filter :set_cache_buster 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"] = "Fri, 01 Jan 1990 00:00:00 GMT" end end` – Vatsal Singh Jul 05 '12 at 06:48
  • You forgot the line `before_filter :set_no_cache` in your before_filters. Add that and it should work. – Danish M. Jul 05 '12 at 06:50
  • Danish, it still doesn't work! I pasted the code you have given exactly. What else can I do? And thanx a zillion for trying to help me out. :) – Vatsal Singh Jul 05 '12 at 06:54
  • Have you tried restarting your rails server? And no problem. Also, it may help to clear the cache of your web browser. – Danish M. Jul 05 '12 at 06:56
  • Ya, I restarted my server and now this is how my code looks like `class ApplicationController < ActionController::Base protect_from_forgery include SessionsHelper before_filter :set_no_cache def set_no_cache response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate" response.headers["Pragma"] = "no-cache" response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT" end end` – Vatsal Singh Jul 05 '12 at 06:59
  • That code looks fine. Are you sure the page is caching? Like if you reload the page after pressing the back button, does it still work or not? If the page still works even after a reload, then it's not a caching problem. – Danish M. Jul 05 '12 at 07:09
  • Danish, the page is getting reloaded after clicking "back" button also. – Vatsal Singh Jul 05 '12 at 07:20
-2

simply redirect to sign in page on sign out button and disable browser back button.

  • Is it okay if I disable the "back" button. I mean, what action would "back" button then have on other pages? – Vatsal Singh Jul 05 '12 at 06:46
  • This is neither practical not possible. First of all, you're [introducing a worst practice by breaking convention](http://www.useit.com/alertbox/990530.html) (the back button) for no real benefit to the user. Secondly, you're ignoring the fact that **all web commands are suggestions**. It is up to the browser whether to follow them. What if the user is using Lynx? Its 'back button' can't be disabled, can it? – BryanH Nov 19 '12 at 17:08