Controlling Sessions
At this point, you can actually create a new user by running script/server
and visiting http://localhost:3000/users/new, as
shown in Figure 14-1.
Figure 14-1. Creating a new user in the default form
While the sign-up will actually workâyou can tell if you check the logsâthe submission just gets redirected to the top-level page, which in this case is still the Rails welcome page. Thatâs not very helpful. (On the bright side, if the passwords donât match, that warning will come through and the form reloads.)
Tip
Keep track of this first accountâit will make things much simpler when itâs time later in the chapter to create an administrator.
Similarly you can log in. Well, you can almost log in. One side effect of using session
rather than sessions
is that a route in config/routes.rb
needs to change from:
map.resource :session
to:
map.resource :session, :controller => 'session'
Otherwise, youâll get a NameError
.
While youâre in the routes.rb file, just
below the line for :session
, add:
map.signup '/signup', :controller => 'users', :action => 'new' map.login '/login', :controller => 'session', :action => 'new' map.logout '/logout', :controller => 'session', :action => 'destroy'
Once this is set up, you can connect to http://localhost:3000/login and find the login page shown in Figure 14-2.
Figure 14-2. Logging in for the first time
A successful login, once ...
Get Learning Rails: Live Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.