19: Routes - Who sees what?
If you configured registration and sign in buttons on your landing page, you may feel confused on how the actually work, since it seems that users do not manage to sign in.
Actually, what happens is that if you're already authenticated, you see the landing page, and when you try to sign in... you are redirected to the same page.
We'd like a different logic here.
So:
- If the user is not signed in, when they go to the root of the website (
casemanagementsystem.md
orlocalhost:3000
), they should see the landing page - If the user is signed in, when they go to the root of the website, they should see the Cases page.
Remember, we have a before filter in the Cases controller that prevents unauthorized access to the Cases Index page.
To implement this logic, we need to change in config/routes.rb
the previous root to: pages#landing
to a more complex structure:
authenticated :user do
root to: "cases#index", as: :authenticated_root
end
root to: "pages#landing"
This tells Rails that authenticated users will see the Index view of Cases and everyone else will see the Landing view of Pages.
Finally, this is the whole content of my config/routes.rb
file:
Rails.application.routes.draw do
mount Attachinary::Engine => '/attachinary'
devise_for :users
resources :cases
authenticated :user do
root to: "cases#index", as: :authenticated_root
end
root to: "pages#landing"
get "features" => "pages#features"
get "landing" => "pages#landing"
end