1.安裝Ruby
去http://rubyforge.org/frs/?group_id=167 下載一個最新的Ruby一鍵安裝包,安裝的時候別忘記Enable RubyGems
安裝完成之后使用
ruby -v
gem -v
來測試是否安裝成功.
2.安裝Rails
使用 gem install rails 自動安裝Rails
使用 gem install mysql 自動安裝Mysql(注意下版本,window下就用win32那個).
使用rails -v來測試是否安裝成功.
3.小試牛刀,使用RoR來創(chuàng)建一個超級簡單應(yīng)用
使用rails blog -d mysql創(chuàng)建一個簡單的帶數(shù)據(jù)庫(mysql)的應(yīng)用(-d 參數(shù)后面就是數(shù)據(jù)庫類型如mysql,postgresql和sqlite3等等).
執(zhí)行命令完了之后Rails就會幫我建立基本的目錄,其中config目錄下就是所有配置文件對用的目錄,數(shù)據(jù)庫的相關(guān)配置是database.yml .
[CODE]
##開發(fā)用服務(wù)器
development:
adapter: mysql
encoding: utf8
database: blog_development
username: root
password:
host: localhost
##測試用服務(wù)器
test:
adapter: mysql
encoding: utf8
database: blog_test
username: root
password:
host: localhost
##生產(chǎn)庫
production:
adapter: mysql
encoding: utf8
database: blog_production
username: root
password:
host: localhost
[CODE]
我們在這里配置development數(shù)據(jù)庫用于開發(fā)用.
使用 ruby script/generate controller home index 產(chǎn)生一個簡單控制器home和一個index方法.
[CODE]
>ruby script/generate controller home index
exists app/controllers/
exists app/helpers/
create app/views/home
exists test/functional/
create app/controllers/home_controller.rb
create test/functional/home_controller_test.rb
create app/helpers/home_helper.rb
create app/views/home/index.html.erb
[CODE]
使用 >ruby script/server啟動測試服務(wù)器,在瀏覽器中鍵入 http://localhost:3000 就可以看見測試頁面.
刪除 public\index.html 文件,然后打開config/routes.rb文件,在里面添加map.root :controller => "home" 這樣就可以把默認(rèn)首頁跳轉(zhuǎn)到home下面.
使用 script/generate scaffold 命令可以生成一個支持?jǐn)?shù)據(jù)庫增刪改操作的類,使用Migrations 類可以方便快捷地操作數(shù)據(jù)庫表.
使用 >ruby script/generate scaffold Post name:string title:string content:text 生成一個簡單文字列表應(yīng)用.
改命令出了POST相關(guān)增刪改方法之外還會產(chǎn)生數(shù)據(jù)庫腳本.
[CODE]
db/migrate
db/migrate/20081121132411_create_posts.rb
[CODE]
使用命令
>rake db:create
>rake db:migrate
可以創(chuàng)建相應(yīng)的數(shù)據(jù)庫和表.
在訪問http://localhost:3000/posts 就可以看見主頁了