1. Computing

Rails Blog Tutorial - Adding RESTful Authentication

By

4 of 8

Creating Users

Before you can create users, you have to create the database table to hold the users. Since the authenticated generator already created a migration for you, simply run the migration by running the db:migrate rake task.

$ rake db:migrate
 == 20080723175229 CreateUsers: migrating ========
 -- create_table("users", {:force=>true})
    ;-> 0.0061s
 == 20080723175229 CreateUsers: migrated (0.0064s)

Once the migration is run, you can start up the console and create a new user. A new user can be created simply by creating and saving a new User object with the correct parameters. Any future users can be added in this way. Be sure that the save method returns true, as if it returns false it means there were errors.

$ script/console
 Loading development environment (Rails 2.1.0)
 >> u = User.new :login => 'admin',
 ?> :email => 'email@email.com',
 ?> :password => '12345',
 ?> :password_confirmation => '12345'
 =>
 >> u.save
 => true
 >> u
 =>
 >>
  1. About.com
  2. Computing
  3. Ruby
  4. Ruby on Rails
  5. Creating Users

©2014 About.com. All rights reserved.