O'Reilly logo

Learning Rails by Edd Dumbill, Simon St. Laurent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 again, returns the user ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required