SlideShare ist ein Scribd-Unternehmen logo
1 von 64
Ruby on Raila Tutorial Part I

       Author: Lu Wei Jen
Agenda
•   Ruby and Rails on Windows
•   Build a Simple DailyLog Application
•   The Rails Framework
•   Restful
•   Writing Code with Unit Test
Install Ruby and Rails on Windows(1)
• BitNami RubyStack Installer.
• http://bitnami.org/stack/rubystack
• Included
  –   Ruby
  –   Rubygems
  –   Rails
  –   Git
  –   Mysql
  –   Apache
  –   Some useful gems
Install Ruby and Rails on Windows(2)
• One-Click Ruby Installer
• http://rubyforge.org/projects/rubyinstaller/
• Only install ruby and rubygems.
Ruby and Rails IDE
• NetBeans
  – http://www.netbeans.org/
• Aptana
  – http://www.aptana.com/
Sqlite on Windows
• Ruby on Rails 預設使用的資料庫系統。
• 在開發時更容易入手的資料庫系統。
• 要改成MySQL等資料庫系統,只需安裝相對
  應的adapter plugin,與換掉database.yml檔
  案即可。
• Sqlite官網: http://www.sqlite.org/
Git on Windows
• Ruby on Rails 官方的版本控制系統
• GitHub 一大堆有的沒有的ruby or rails plugin
  存放的地方
• Git 官網 http://git-scm.com/
Update Rails to 2.3.2.x
• Input the following commands
  – gem install rubygems-update
  – update_rubygems
  – gem insatll rails
  – 如果你使用Vista,請關閉UAC
Create DailyLog Application
• User Story
  – 使用者可以記錄每一筆收入與支出。
  – 使用者可以瀏覽所有收入與支出。
  – 可以刪除某筆收入或支出紀錄。
  – 使用者可以瀏覽收入總額與支出總額。
Create a New Project
• In NetBeans: File -> New Project
Create a New Project(2)
• Setup the project name and ruby platform
Create a New Project(3)
• Setup database
Create a New Project(4)
• Select rails version.
The Rails Framework
Rails files’ viewpoint     Rails projects’ viewpoint
The Rails Framework - MVC (1)
• MVC – Model, View, Controller
• Model:
  – 負責處理數據與邏輯。
  – 與資料庫系統的連接。
• View:
  – 負責處理使用者介面。
• Controller:
  – 負責處理使用者請求。
  – Model 與 view間的橋梁。
The Rails Framework - MVC (2)
The Rails Framework - config Folder
                  • environment.rb:
                     – 系統所有模式均需要的組
                       態程式。
                  • database.yml:
                     – 資料庫系統相關設定
                        。
                  • routes.rb:
                     – 使用者需求與系統路徑的
                       對應(map)。
                  • environments
                     – 在某種執行模式時,所需
                       要的組態程式。
The Rails Framework – Others(1)
                • db: 資料庫資料表模型
                  與migration。
                • doc: 系統相關文件
                • lib: 放置函式庫,例如
                  Façade。
                • log: 系統執行紀錄。
                • public: 放置靜態的web
                  檔案,如html, 圖
                  片, javascript, css檔案
The Rails Framework – Others(1)
                • script: 啟動與管理rails
                  的一些工具程式。
                • test: 放置rails的測試程
                  式。
                • tmp: 放置某些在處理
                  過程中所需的暫時性
                  文件。
                • vendor: 別人寫的程式
                  庫或者工具。
Let’s Write Code
Scaffold
Create Database
Run Web Server
Home Page
Index Page
Create, Update
Show an Item
Delete an Item
What’s Happened
Database Configuration
  RAILS_ROOT/config/database.yml
Migration(1)
•   用Ruby來寫Database schema。
•   依序執行,也可以依序退回,方便維護。
•   跨資料庫系統的資料型態支援。
•   支援的資料型態:
    – binary, boolean, date, datetime, decimal, float, int
      eger, string, text, time, timestamp.
Migration (2)
-RAILS_ROOT/db/migrate/20090415170848_create_accounts.rb
Model
-RAILS_ROOT/app/models/account.rb
Controllers (1)
Controllers (2)
-RAILS_ROOT/app/controllers/application_controller.rb
Controllers (3)
-RAILS_ROOT/app/controllers/account_controller.rb




                           The Actions
Restful (1)
• Restful是一個好的方式來針對controllers與
  actions命名。
• Restful是一個好的方式來區隔Service。
 – 我們要提供什麼樣的service?
 – 如何針對這個service做操作? (CRUD)


• Rails 2.x後,route的機制都預設為Rest。
Restful (2)
  HTTP METHODS                      SERVICE METHODS

       POST                              CREATE

 POST + /accounts/               Create an account record
        GET                               READ

 GET + /accounts/1          Show an account record which id = 1
        PUT                              UPDATE

 PUT + /accounts/1         Update an account record which id = 1
      DELETE                              DELETE

DELETE + /accounts/1       Delete an account record which id = 1
Restful - route
-RAILS_ROOT/config/routes.rb




• routes.rb 就像是系統的地圖,負責將使用者的需
  求導引到正確的地方。
• 開發者要提供services時,需要在這裡進行註
  冊。
• map.resources 一共會提供7個actions。
Action & View
• Action: 處理使用者要求的地方。
• View: 回應使用者要求的介面。
• 在RAILS_ROOT/app/views 下面會有與
  controller同名的目錄,存放介面檔案。
Action & View – index (1)
-RAILS_ROOT/app/controllers/accounts_controllers
- http://localhost:3000/accounts
Action & View – index (2)
• 同樣一個action,不一定只能回應(response)
  一種介面樣式。
• Ex. http://localhost:3000/accounts.xml => 回
  傳xml的介面樣式。
• 減少了重複的程式碼。
• 更容易跨平台。
Action & View – index (3)
-RAILS_ROOT/app/views/accounts/index.html.erb
- http://localhost:3000/accounts
URL Helpers
     Methods
                                 GET            POST          PUT         DELETE
Helpers

                              /accounts/1                  /accounts/1   /accounts/1
 account_path(@account)          show                        update         delete

                               /accounts       /accounts
      accounts_path              index           create

                            /accounts/1/edit
edit_event_path(@account)         edit

                            /accounts/new
    new_events_path
                                 new
Action & View - new
Action & View - create
Helper
• 在View中,會被重複使用到的函式可以放
  在這裡。
• 例如: 時間表示格式,金額表示格式。
Helper - Example
-RAILS_ROOT/app/helpers/accounts_helper.rb
Unit Test on Rails
Why You Need Unit Test
• 更容易修改或者改善你的程式碼。
• 更容易與其他系統整合。
• 測試程式碼就是你的文件(的一部分)。
• 你在寫測試程式時,你就是在設計你的目
  的程式。(Writing tests before writing the
  code being tested.)
• Rails社群提供了很好的工具讓你進行Unit
  Test。
RSpec plugin
• RSpec 官網: http://rspec.inof
• Install RSpec, Rspec-rails on Rails 2.3.x
   – gem install rspec rspec-rails
• Generate RSpec framework for your project
我該怎麼測試
• User Story: 呈現我的支出總額
 – 我先製造10筆紀錄,其中五筆是收入,五筆式
   支出。
 – 我需要一個Account.total_outgoing的函式來呈
   現支出總額。
 – 我會把10筆紀錄都從資料庫中撈進來,然後把
   每一筆記錄的outgoing欄位加總。
 – Account.total_outgoing 的支出總額應該是
   1500.0元
Fixtures
Write RSpec Code
第一次的測試




•NoMethodError 因為我們還沒寫實際的目的程式
第二次的測試




•有 total_outgoing 程式了,可是回傳不對,是時候寫目的程式了。
第一次寫的目的程式
第三次的測試




•仍然有錯,原因是有些紀錄中,outgoing 是 nil。
第二次寫的目的程式
第四次的測試




•終於成功了。
•一次把所有資料都撈進來然後計算好像很笨耶。
•我需要Refactor。
Refactor
•   Red – Green - Refactor
•   第一次寫的程式是為了達到目的。
•   第二、三…次寫的程式是為了更好。
•   Unit Test 可以確保你的修改不影響結果。
第三次寫的目的程式
第五次的測試




•我們有了比較好的效能了。
•我們還可以更好。
•ㄟ~~,下次好了。
End

謝謝大家

Weitere ähnliche Inhalte

Was ist angesagt?

421 Ch
421 Ch421 Ch
421 Chanjaan
 
Diving Into The Yahoo Open Stack
Diving Into The Yahoo Open StackDiving Into The Yahoo Open Stack
Diving Into The Yahoo Open StackDustin Whittle
 
20081128 Bp Study#15 Active Record
20081128 Bp Study#15 Active Record20081128 Bp Study#15 Active Record
20081128 Bp Study#15 Active RecordTomohito Ozaki
 
Text Mining and SEASR
Text Mining and SEASRText Mining and SEASR
Text Mining and SEASRLoretta Auvil
 
Blueprint talk at Open Hackday London 2009
Blueprint talk at Open Hackday London 2009Blueprint talk at Open Hackday London 2009
Blueprint talk at Open Hackday London 2009Ricardo Varela
 
Yahoo Innovation Culture And Hacker
Yahoo Innovation Culture And HackerYahoo Innovation Culture And Hacker
Yahoo Innovation Culture And HackerJinho Jung
 
『Ficia』インフラとPerlにまつわるエトセトラ
『Ficia』インフラとPerlにまつわるエトセトラ『Ficia』インフラとPerlにまつわるエトセトラ
『Ficia』インフラとPerlにまつわるエトセトラMasaaki HIROSE
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Joao Lucas Santana
 
Private slideshow
Private slideshowPrivate slideshow
Private slideshowsblackman
 
Flickr Open Api Mashup
Flickr Open Api MashupFlickr Open Api Mashup
Flickr Open Api MashupJinho Jung
 
Optaros Surf Code Camp Lab 1
Optaros Surf Code Camp Lab 1Optaros Surf Code Camp Lab 1
Optaros Surf Code Camp Lab 1Jeff Potts
 

Was ist angesagt? (18)

421 Ch
421 Ch421 Ch
421 Ch
 
Diving Into The Yahoo Open Stack
Diving Into The Yahoo Open StackDiving Into The Yahoo Open Stack
Diving Into The Yahoo Open Stack
 
20081128 Bp Study#15 Active Record
20081128 Bp Study#15 Active Record20081128 Bp Study#15 Active Record
20081128 Bp Study#15 Active Record
 
Text Mining and SEASR
Text Mining and SEASRText Mining and SEASR
Text Mining and SEASR
 
Grails紹介
Grails紹介Grails紹介
Grails紹介
 
Spring Framework勉強会
Spring  Framework勉強会Spring  Framework勉強会
Spring Framework勉強会
 
Blueprint talk at Open Hackday London 2009
Blueprint talk at Open Hackday London 2009Blueprint talk at Open Hackday London 2009
Blueprint talk at Open Hackday London 2009
 
Dev004奚江華
Dev004奚江華Dev004奚江華
Dev004奚江華
 
XS Japan 2008 Citrix Japanese
XS Japan 2008 Citrix JapaneseXS Japan 2008 Citrix Japanese
XS Japan 2008 Citrix Japanese
 
Yahoo Innovation Culture And Hacker
Yahoo Innovation Culture And HackerYahoo Innovation Culture And Hacker
Yahoo Innovation Culture And Hacker
 
『Ficia』インフラとPerlにまつわるエトセトラ
『Ficia』インフラとPerlにまつわるエトセトラ『Ficia』インフラとPerlにまつわるエトセトラ
『Ficia』インフラとPerlにまつわるエトセトラ
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)
 
Private slideshow
Private slideshowPrivate slideshow
Private slideshow
 
misspattern
misspatternmisspattern
misspattern
 
Flickr Open Api Mashup
Flickr Open Api MashupFlickr Open Api Mashup
Flickr Open Api Mashup
 
Optaros Surf Code Camp Lab 1
Optaros Surf Code Camp Lab 1Optaros Surf Code Camp Lab 1
Optaros Surf Code Camp Lab 1
 
Gpl 과 Ccl
Gpl 과  CclGpl 과  Ccl
Gpl 과 Ccl
 
Insertcustomer
InsertcustomerInsertcustomer
Insertcustomer
 

Ähnlich wie Ruby on Rails Tutorial Part I

Ruby on Rails 2.1 What's New Chinese Version
Ruby on Rails 2.1 What's New Chinese VersionRuby on Rails 2.1 What's New Chinese Version
Ruby on Rails 2.1 What's New Chinese VersionLibin Pan
 
Chinaonrails Rubyonrails21 Zh
Chinaonrails Rubyonrails21 ZhChinaonrails Rubyonrails21 Zh
Chinaonrails Rubyonrails21 ZhJesse Cai
 
企业级搜索引擎Solr交流
企业级搜索引擎Solr交流企业级搜索引擎Solr交流
企业级搜索引擎Solr交流chuan liang
 
Rails Cache
Rails CacheRails Cache
Rails Cachewear
 
Web技術勉強会 第19回
Web技術勉強会 第19回Web技術勉強会 第19回
Web技術勉強会 第19回龍一 田中
 
yusukebe in Yokohama.pm 090909
yusukebe in Yokohama.pm 090909yusukebe in Yokohama.pm 090909
yusukebe in Yokohama.pm 090909Yusuke Wada
 
4200 Kte7.0 Training V1.0
4200 Kte7.0 Training V1.04200 Kte7.0 Training V1.0
4200 Kte7.0 Training V1.0wayneliao
 
ブラウザでMap Reduce風味の並列分散処理
ブラウザでMap Reduce風味の並列分散処理ブラウザでMap Reduce風味の並列分散処理
ブラウザでMap Reduce風味の並列分散処理Shinya Miyazaki
 
Gorm @ gopher china
Gorm @ gopher chinaGorm @ gopher china
Gorm @ gopher chinaJinzhu
 
Understanding Web Services
Understanding Web ServicesUnderstanding Web Services
Understanding Web Servicesaru85
 
Understanding Web Services
Understanding Web ServicesUnderstanding Web Services
Understanding Web Servicesaru85
 
20090323 Phpstudy
20090323 Phpstudy20090323 Phpstudy
20090323 PhpstudyYusuke Ando
 
Spring基础教程
Spring基础教程Spring基础教程
Spring基础教程Shilong Sang
 
Five Minutes Introduction For Rails
Five Minutes Introduction For RailsFive Minutes Introduction For Rails
Five Minutes Introduction For RailsKoichi ITO
 
Ds 008 方法設計之技術產品流程
Ds 008 方法設計之技術產品流程Ds 008 方法設計之技術產品流程
Ds 008 方法設計之技術產品流程handbook
 
2007 0822 Antelope Php
2007 0822 Antelope Php2007 0822 Antelope Php
2007 0822 Antelope Phpgmaxsonic
 
090309seminar talk about Cloud Computing
090309seminar talk about Cloud Computing090309seminar talk about Cloud Computing
090309seminar talk about Cloud ComputingKohei Nishikawa
 

Ähnlich wie Ruby on Rails Tutorial Part I (20)

Ruby on Rails 2.1 What's New Chinese Version
Ruby on Rails 2.1 What's New Chinese VersionRuby on Rails 2.1 What's New Chinese Version
Ruby on Rails 2.1 What's New Chinese Version
 
Chinaonrails Rubyonrails21 Zh
Chinaonrails Rubyonrails21 ZhChinaonrails Rubyonrails21 Zh
Chinaonrails Rubyonrails21 Zh
 
企业级搜索引擎Solr交流
企业级搜索引擎Solr交流企业级搜索引擎Solr交流
企业级搜索引擎Solr交流
 
Rails Cache
Rails CacheRails Cache
Rails Cache
 
Web技術勉強会 第19回
Web技術勉強会 第19回Web技術勉強会 第19回
Web技術勉強会 第19回
 
Revisited
RevisitedRevisited
Revisited
 
yusukebe in Yokohama.pm 090909
yusukebe in Yokohama.pm 090909yusukebe in Yokohama.pm 090909
yusukebe in Yokohama.pm 090909
 
4200 Kte7.0 Training V1.0
4200 Kte7.0 Training V1.04200 Kte7.0 Training V1.0
4200 Kte7.0 Training V1.0
 
ブラウザでMap Reduce風味の並列分散処理
ブラウザでMap Reduce風味の並列分散処理ブラウザでMap Reduce風味の並列分散処理
ブラウザでMap Reduce風味の並列分散処理
 
Gorm @ gopher china
Gorm @ gopher chinaGorm @ gopher china
Gorm @ gopher china
 
Understanding Web Services
Understanding Web ServicesUnderstanding Web Services
Understanding Web Services
 
Understanding Web Services
Understanding Web ServicesUnderstanding Web Services
Understanding Web Services
 
20090323 Phpstudy
20090323 Phpstudy20090323 Phpstudy
20090323 Phpstudy
 
spring_jiaocheng
spring_jiaochengspring_jiaocheng
spring_jiaocheng
 
Spring基础教程
Spring基础教程Spring基础教程
Spring基础教程
 
Five Minutes Introduction For Rails
Five Minutes Introduction For RailsFive Minutes Introduction For Rails
Five Minutes Introduction For Rails
 
Ds 008 方法設計之技術產品流程
Ds 008 方法設計之技術產品流程Ds 008 方法設計之技術產品流程
Ds 008 方法設計之技術產品流程
 
2007 0822 Antelope Php
2007 0822 Antelope Php2007 0822 Antelope Php
2007 0822 Antelope Php
 
090309seminar talk about Cloud Computing
090309seminar talk about Cloud Computing090309seminar talk about Cloud Computing
090309seminar talk about Cloud Computing
 
Seize The Cloud
Seize The CloudSeize The Cloud
Seize The Cloud
 

Ruby on Rails Tutorial Part I