Search code examples
ruby-on-railsdevise

How to make devise not require authentication for home page?


I have before_action :authenticate_user! in the application controller, which is very efficient because it applies to all controllers but there's one page (home page) that should be available without login.

Is there a way to keep before_action :authenticate_user! in application_controller.rb (for the sake of DRY) and make an exception to allow access to just home#home to be available without authentication?

Notes:

  • we could obviously remove before_action :authenticate_user! from the application controller and place it in all controllers but for 'home#home' (the downside to this is it's not very DRY)
  • just a side note, but I'm not exactly sure why the /users/sign_in url is even accessible (something in devise must make it so, but I'm not sure where that mechanic lives or what it is exactly - perhaps if I found it I could add more routes to it?)

Solution

  • You want to skip the before_action using skip_before_action

    In the controller for that page put

    skip_before_action :authenticate_user!, only: [:home]
    

    This specifies it just for that one action, if all the pages in that controller need to be skipped you can just use

    skip_before_action :authenticate_user!