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.

Creating a new user in the default form

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.