Avoid Getting An Error Message On Signout In Devise

Pretty much every Rails app I have ever worked with uses Devise for Authentication. I think it’s a great library.

If your entire site is behind a login wall you will most likely run into an annoying issue: after user logout you get an error message.

You need to sign in or sign up before continuing

This happens because by default Devise will redirect you back to the root path, but since the user is unauthenticated they are once again redirected to the login path with an error message saying they need to sign in.

As with most things in Devise this is easy to fix - we simply need to customize the after_sign_out_path_for method.

class ApplicationController < ActionController::Base
  def after_sign_out_path_for(*)
    new_user_session_path
  end
end

Now you will get a nice success message.

Signed out successfully.