I want users#new and tasks#index to display as the root path URL, i.e. / When a user logs in on the path users#new (set as root) they are redirected to tasks#index and URL does not change. Can this be done in the routes.rb file?
This is my routes.rb file:
Todo::Application.routes.draw do
resources :sessions
resources :subscriptions
resources :users
resources :tasks do
collection do
post :sort
end
end
root :to => "users#new"
match "sessions#new" => "tasks#index"
match "sessions#" => "tasks#index"
I’m not sure I would actually do this,but assuming you map the root tasks#index, you could also use a before filter to render the sessions#new template when the user is not logged in:
To be clear, when a user requests “/”, Rails will route this request to a single controller and action. You can map multiple paths to the same controller/action, but a single path is deterministic to a particular controller/action. IOW – it is not logically possible in routes to have a get request for any path, root or otherwise, go to more than one controller/action.
Users don’t see a controller and action though, they see the result of what the action renders, usually based on some template, and that you can determine in the action (or a controller filter’s) logic, as I did above.
You could also create a 3rd controller, a RootController, that contains the logic to display the list of tasks or a login page based on if the user is logged in.