2008年3月18日 星期二

build a basic web site with ruby on rails

1. use rail to create default directories and files
rails -d mysql bookStore
-d mysql: use mysql as database

2. Tell rail information about database
configure config/database.yml
(1) first version
development:
adapter: mysql
encoding: utf8
database: book_development
username: deeplove
password: 111
socket: /tmp/mysql.sock

test:
adapter: mysql
encoding: utf8
database: book_test
username: deeplove
password: 111
socket: /tmp/mysql.sock

production:
adapter: mysql
encoding: utf8
database: book_production
username: deeplove
password: 111
socket: /tmp/mysql.sock
----------------------------------
(2) simpler version
defaults: &defaults
adapter: mysql
encoding: utf8
username: deeplove
password: 111
socket: /tmp/mysql.sock

development:
database: book_development
<<: *defaults

test:
database: book_test
<<: *defaults

production:
database: book_production
<<: *defaults

3. create the database
rake db:create:all
rake for ruby is like make for C

useful rake tasks:
(1)create:all :
Create all the local databases defined in config/database.yml

(2)db:drop:all
Drops all the local databases defined in config/database.yml

(3)db:rollback
Rolls the schema back to the previous version. Specify the number of steps with STEP=n

(4)db:version
Retrieves the current schema version number

4. use scaffolding to create view, model and controller for the table

script/generate scaffold Book title:string date:datetime
db/migrate/001_create_books.rb is created
The content:

class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.string :title
t.datetime :date

t.timestamps
end
end

def self.down
drop_table :books
end
end
--------------------------------
to create table or modify table:
up method

roll back to state before migration
down method
ex: change state from 002 to 001

perform the migration(create the table actually)
rake db:migrate
remember to start sql server before do migration

5. add column to existing table:

add money column on books table
(1)script/generate migration AddMoneyToBook
db/migrate/002_add_money_to_book.rb are created

(2)edit 002_add_money_to_book.rb
class AddMoneyToBook < ActiveRecord::Migration
def self.up
add_column :books, :money, :string
end

def self.down
remove_column :books, :money
end
end

(3)rake db:migrate

ps: how to roll back:
rake db:rollback
or
rake db:rollback STEP=n

6. set the route
route define what controller and action respond to url you enter
when the url's page is not in the public folder, it will find for controller and action
format: hostname/controller/action
ex:
htpp://127.0.0.1:3000/books/list
books is controller, list is action

htpp://127.0.0.1:3000/books
when no action name, index is the default

what if "htpp://127.0.0.1:3000"
(1)remove index.html in public folder
(2)add following line in config/routes.rb
map.root :controller => 'books'
map.root is a shorthand to name a route for the root path

7. run web server to test

run WEBrick Web server
script/server
the server is waiting connection on 127.0.0.1:3000

8. visit the website
http://127.0.0.1:3000
the page shown is in public/index.html


9. modify controller action and views

modify app/controllers/books_controller.rb
ex: add new method

def index
list
render :action => 'list'
end

def list
@books = Book.find(:all)
end
Book is the model name, find(:all) return all rows from books table in @books array

add app/views/books/list.rhtml

listing books

沒有留言: