ICS 492 Ruby on Rails
Spring 2008
Practice Week #3
Create A Rails Application
1. Start up InstantRails
2. Create a rails application
rails zoo
cd zoo
Configure Your DBMS
1. Check to be sure that your database configuration file looks like this (but only if you are using SQLite3). Most likely Rails will set up your database.yml file correctly for you. However, if you are using MySQL, you may have to add a valid username and password to the file.
File: …/rails_apps/zoo/config/database.yml
# SQLite version 3.x
# gem install
sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database:
db/development.sqlite3
timeout: 5000
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run
'rake'.
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database:
db/test.sqlite3
timeout: 5000
production:
adapter: sqlite3
database:
db/production.sqlite3
timeout: 5000
(Optional) You can change the name of the database file if you like.
Create a Migration
1. Generate the model and migration
ruby script/generate model Pet
2. Edit the migration file to create the database schema
Filename: …/rails_apps/zoo/db/migrate/001_create_pets.rb
# ORIGINAL
class CreatePets < ActiveRecord::Migration
def self.up
create_table :pets do
|t|
t.timestamps
end
end
def self.down
drop_table :pets
end
end
Change to:
class CreatePets < ActiveRecord::Migration
def self.up
create_table :pets do
|t|
t.column
"name", :string
t.column
"breed", :string
t.column
"age", :integer
end
end
def self.down
drop_table :pets
end
end
That is, add a column for each column to be created in the database table.
3. Create the database.
rake db:migrate
4. Check to be sure your database was correctly created.
cd db
sqlite3 development.sqlite3
.tables
.schema
5. Look for the version of the schema you are currently using.
select * from schema_info;
Populate Your Database
1. Create a file of SQL commands to populate your database.
insert into pets values (1, 'Fido', 'Persian', 15);
insert into pets values ( 2, 'Fluffy', 'Border Collie', 4);
insert into pets values (3, 'Stretch', 'Boa constrictor', 2);
insert into pets values (4, 'Huggy', 'Black bear', 5);
Save the file in your db directory.
2. Submit the SQL commands to your database.
sqllite3 development.sqlite3 < yoursqlcommands.sql
3. Again verify that your data was properly entered into the database.
Migrate Down
1. For practice, migrate back to version 0. This should destroy your database.
rake db:migrate VERSION=0
2. Check to be sure your database table was destroyed.
sqlite3 development.sqlite3
.tables
.schema
3. Look for the version of the schema you are currently using.
select * from schema_info;
4. Migrate back up again and repopulate your database table.
Generate a View and a Controller
1. Generate a controller (you should be in the zoo directory when giving this command).
ruby script/generate controller Pet
2. Generate a view.
ruby script/generate controller Pet index
3. Start up the application from InstantRails
from the black I, click Rails Applications -> Manage Rails Applications
select zoo
Start with Mongrel
4. Open a browser to make sure things are set up properly.
You should see the “Welcome Aboard” message.
Accessing the Database and Displaying the Data
1. Add an index method to your controller file, …/rails_apps/zoo/app/controllers/pet_controller.rb
def
index
@onePet = Pet.find(1)
end
2. Display the data from the view file, …/rails_apps/zoo/app/views/pet/index.html.erb
<%=
@onePet.name %>
3. Take a look at your application from the browser.
You should see the name of the first pet in the database – Fido
Congratulations! You’ve just completed your first Rails application!
For Practice
Using the Rails Console
1. Practice or test your Active Rails commands interactively using the Rails console.
cd zoo
ruby script/console
>> mypet=Pet.new
>> mypet.name=”Honey”
>> mypet.breed=”Rabbit”
>> mypet.age = 2
>> mypet.save
>> puts mypet.name
>> nextpet = Pet.find_by_breed(“Giraffe”)
>> exit