Skip to Main Content
Learning Rails
book

Learning Rails

by Simon St. Laurent, Edd Dumbill
November 2008
Beginner content levelBeginner
448 pages
12h 25m
English
O'Reilly Media, Inc.
Content preview from Learning Rails

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Rails 5

Learning Rails 5

J. Mark Locklear, Eric J Gruber, Barnabas Bulpett
Mastering Ruby Closures

Mastering Ruby Closures

Benjamin Tan Wei Hao
Ruby on Rails® Bible

Ruby on Rails® Bible

Timothy Fisher

Publisher Resources

ISBN: 9780596154943Supplemental ContentErrata