haseeb haseeb - 4 months ago 15x
Ruby Question

Rails Devise - Define separate root paths for signed in user and anonymous user

I want the

of a signed_in user to be
and users that are not signed in user to be

I am using Devise and I know there is a helper
but I dont understand how to use it for this purpose.

How do I make this work?


You can use this:

unauthenticated :user do
  root :to => 'main#index'

authenticated :user do
  # Rails 3 syntax
  # root :to => "main#dashboard"   
  # Rails 4 requires the 'as' option to give it a unique name
  root :to => "main#dashboard", :as => "authenticated_root"

This is the mechanism provided by Devise for re-rooting authenticated users.

The root :to => 'main#index' line is the standard line that Rails places in your config/routes.rb when the application is first created. You can wrap it in the unauthenticated :user do .. end block to make certain that it applies to users that are not logged in.

This gives definitive root routing for all users, based on whether they're logged in.