Organisation of routes in Rails — restful_authentication, session and /login
-
03-07-2019 - |
Question
I am struggling with the route setup for a Rails application. I have installed restful_authentication and mostly followed the instructions. I have set up the routes this way:
map.login '/login', :controller => 'sessions', :action => 'new'
map.logout '/logout', :controller => 'sessions', :action => 'destroy'
map.resource :session
If you're not logged in, you're redirected to http://localhost:3000/session/new.
It makes some kind of sense, as the code in lib/authenticated_system.rb says redirect_to new_session_path
.
But I thought the routes mapping was supposed to work both ways (code to URL and URL to code). Can someone explain? Thanks
Solution
map.resource :session
creates a few named resources for you including new_session_path
(see ActionController::Resources).
map.login
and map.logout
are just helper routes to make your code easier to understand. map.login
(which generates login_path
) points to the same controller/action combo as new_session_path
does, it's just easier to remember at a glance what it does.