ruby on rails tutorial part i

64
Ruby on Raila Tutorial Part I Author: Lu Wei Jen

Upload: wei-jen-lu

Post on 14-May-2015

2.864 views

Category:

Documents


3 download

DESCRIPTION

The Ruby on Rails Tutorial for OSDC 09。

TRANSCRIPT

Page 1: Ruby on Rails Tutorial Part I

Ruby on Raila Tutorial Part I

Author: Lu Wei Jen

Page 2: Ruby on Rails Tutorial Part I

Agenda

• Ruby and Rails on Windows• Build a Simple DailyLog Application• The Rails Framework• Restful• Writing Code with Unit Test

Page 3: Ruby on Rails Tutorial Part I

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

Page 4: Ruby on Rails Tutorial Part I

Install Ruby and Rails on Windows(2)

• One-Click Ruby Installer• http://rubyforge.org/projects/rubyinstaller/• Only install ruby and rubygems.

Page 5: Ruby on Rails Tutorial Part I

Ruby and Rails IDE

• NetBeans – http://www.netbeans.org/

• Aptana– http://www.aptana.com/

Page 6: Ruby on Rails Tutorial Part I

Sqlite on Windows

• Ruby on Rails 預設使用的資料庫系統。• 在開發時更容易入手的資料庫系統。• 要改成 MySQL 等資料庫系統,只需安裝相

對應的 adapter plugin ,與換掉database.yml 檔案即可。

• Sqlite 官網 : http://www.sqlite.org/

Page 7: Ruby on Rails Tutorial Part I

Git on Windows

• Ruby on Rails 官方的版本控制系統• GitHub 一大堆有的沒有的 ruby or rails

plugin 存放的地方• Git 官網 http://git-scm.com/

Page 8: Ruby on Rails Tutorial Part I

Update Rails to 2.3.2.x

• Input the following commands– gem install rubygems-update – update_rubygems– gem insatll rails– 如果你使用 Vista ,請關閉 UAC

Page 9: Ruby on Rails Tutorial Part I

Create DailyLog Application

• User Story– 使用者可以記錄每一筆收入與支出。– 使用者可以瀏覽所有收入與支出。– 可以刪除某筆收入或支出紀錄。– 使用者可以瀏覽收入總額與支出總額。

Page 10: Ruby on Rails Tutorial Part I

Create a New Project• In NetBeans: File -> New Project

Page 11: Ruby on Rails Tutorial Part I

Create a New Project(2)• Setup the project name and ruby platform

Page 12: Ruby on Rails Tutorial Part I

Create a New Project(3)• Setup database

Page 13: Ruby on Rails Tutorial Part I

Create a New Project(4)• Select rails version.

Page 14: Ruby on Rails Tutorial Part I

The Rails FrameworkRails files’ viewpoint Rails projects’ viewpoint

Page 15: Ruby on Rails Tutorial Part I

The Rails Framework - MVC (1)

• MVC – Model, View, Controller• Model: – 負責處理數據與邏輯。– 與資料庫系統的連接。

• View: – 負責處理使用者介面。

• Controller: – 負責處理使用者請求。– Model 與 view 間的橋梁。

Page 16: Ruby on Rails Tutorial Part I

The Rails Framework - MVC (2)

Page 17: Ruby on Rails Tutorial Part I

The Rails Framework - config Folder

• environment.rb: – 系統所有模式均需要的組

態程式。• database.yml:

– 資料庫系統相關設定。• routes.rb:

– 使用者需求與系統路徑的對應 (map) 。

• environments– 在某種執行模式時,所需

要的組態程式。

Page 18: Ruby on Rails Tutorial Part I

The Rails Framework – Others(1)

• db: 資料庫資料表模型與 migration 。

• doc: 系統相關文件• lib: 放置函式庫,例如

Façade 。• log: 系統執行紀錄。• public: 放置靜態的

web 檔案,如 html, 圖片 , javascript, css 檔案

Page 19: Ruby on Rails Tutorial Part I

The Rails Framework – Others(1)

• script: 啟動與管理 rails的一些工具程式。

• test: 放置 rails 的測試程式。

• tmp: 放置某些在處理過程中所需的暫時性文件。

• vendor: 別人寫的程式庫或者工具。

Page 20: Ruby on Rails Tutorial Part I

Let’s Write Code

Page 21: Ruby on Rails Tutorial Part I

Scaffold

Page 22: Ruby on Rails Tutorial Part I

Create Database

Page 23: Ruby on Rails Tutorial Part I

Run Web Server

Page 24: Ruby on Rails Tutorial Part I

Home Page

Page 25: Ruby on Rails Tutorial Part I

Index Page

Page 26: Ruby on Rails Tutorial Part I

Create, Update

Page 27: Ruby on Rails Tutorial Part I

Show an Item

Page 28: Ruby on Rails Tutorial Part I

Delete an Item

Page 29: Ruby on Rails Tutorial Part I

What’s Happened

Page 30: Ruby on Rails Tutorial Part I

Database Configuration RAILS_ROOT/config/database.yml

Page 31: Ruby on Rails Tutorial Part I

Migration(1)

• 用 Ruby 來寫 Database schema 。• 依序執行,也可以依序退回,方便維護。• 跨資料庫系統的資料型態支援。• 支援的資料型態 : – binary, boolean, date, datetime, decimal, float,

integer, string, text, time, timestamp.

Page 32: Ruby on Rails Tutorial Part I

Migration (2)-RAILS_ROOT/db/migrate/20090415170848_create_accounts.rb

Page 33: Ruby on Rails Tutorial Part I

Model-RAILS_ROOT/app/models/account.rb

Page 34: Ruby on Rails Tutorial Part I

Controllers (1)

Page 35: Ruby on Rails Tutorial Part I

Controllers (2)-RAILS_ROOT/app/controllers/application_controller.rb

Page 36: Ruby on Rails Tutorial Part I

Controllers (3)-RAILS_ROOT/app/controllers/account_controller.rb

The Actions

Page 37: Ruby on Rails Tutorial Part I

Restful (1)

• Restful 是一個好的方式來針對 controllers 與actions 命名。

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

• Rails 2.x 後, route 的機制都預設為 Rest 。

Page 38: Ruby on Rails Tutorial Part I

Restful (2)HTTP METHODS SERVICE METHODS

POST

POST + /accounts/

CREATE

Create an account record

GET

GET + /accounts/1

READ

Show an account record which id = 1

PUT

PUT + /accounts/1

UPDATE

Update an account record which id = 1

DELETE

DELETE + /accounts/1

DELETE

Delete an account record which id = 1

Page 39: Ruby on Rails Tutorial Part I

Restful - route

• routes.rb 就像是系統的地圖,負責將使用者的需求導引到正確的地方。

• 開發者要提供 services 時,需要在這裡進行註冊。• map.resources 一共會提供 7 個 actions 。

-RAILS_ROOT/config/routes.rb

Page 40: Ruby on Rails Tutorial Part I

Action & View

• Action: 處理使用者要求的地方。• View: 回應使用者要求的介面。• 在 RAILS_ROOT/app/views 下面會有與

controller 同名的目錄,存放介面檔案。

Page 41: Ruby on Rails Tutorial Part I

Action & View – index (1)

-RAILS_ROOT/app/controllers/accounts_controllers- http://localhost:3000/accounts

Page 42: Ruby on Rails Tutorial Part I

Action & View – index (2)

• 同樣一個 action ,不一定只能回應(response) 一種介面樣式。

• Ex. http://localhost:3000/accounts.xml => 回傳 xml 的介面樣式。

• 減少了重複的程式碼。• 更容易跨平台。

Page 43: Ruby on Rails Tutorial Part I

Action & View – index (3)-RAILS_ROOT/app/views/accounts/index.html.erb- http://localhost:3000/accounts

Page 44: Ruby on Rails Tutorial Part I

URL Helpers

GET POST PUT DELETE

account_path(@account) /accounts/1show

/accounts/1update

/accounts/1delete

accounts_path /accountsindex

/accountscreate

edit_event_path(@account)

/accounts/1/editedit

new_events_path /accounts/newnew

Methods

Helpers

Page 45: Ruby on Rails Tutorial Part I

Action & View - new

Page 46: Ruby on Rails Tutorial Part I

Action & View - create

Page 47: Ruby on Rails Tutorial Part I

Helper

• 在 View 中,會被重複使用到的函式可以放在這裡。

• 例如 : 時間表示格式,金額表示格式。

Page 48: Ruby on Rails Tutorial Part I

Helper - Example-RAILS_ROOT/app/helpers/accounts_helper.rb

Page 49: Ruby on Rails Tutorial Part I

Unit Test on Rails

Page 50: Ruby on Rails Tutorial Part I

Why You Need Unit Test

• 更容易修改或者改善你的程式碼。• 更容易與其他系統整合。• 測試程式碼就是你的文件 ( 的一部分 ) 。• 你在寫測試程式時,你就是在設計你的目

的程式。 (Writing tests before writing the code being tested.)

• Rails 社群提供了很好的工具讓你進行 Unit Test 。

Page 51: Ruby on Rails Tutorial Part I

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

Page 52: Ruby on Rails Tutorial Part I

我該怎麼測試• User Story: 呈現我的支出總額– 我先製造 10 筆紀錄,其中五筆是收入,五筆式

支出。– 我需要一個 Account.total_outgoing 的函式來呈

現支出總額。– 我會把 10 筆紀錄都從資料庫中撈進來,然後把

每一筆記錄的 outgoing 欄位加總。– Account.total_outgoing 的支出總額應該是

1500.0 元

Page 53: Ruby on Rails Tutorial Part I

Fixtures

Page 54: Ruby on Rails Tutorial Part I

Write RSpec Code

Page 55: Ruby on Rails Tutorial Part I

第一次的測試

•NoMethodError 因為我們還沒寫實際的目的程式

Page 56: Ruby on Rails Tutorial Part I

第二次的測試

•有 total_outgoing 程式了,可是回傳不對,是時候寫目的程式了。

Page 57: Ruby on Rails Tutorial Part I

第一次寫的目的程式

Page 58: Ruby on Rails Tutorial Part I

第三次的測試

•仍然有錯,原因是有些紀錄中, outgoing 是 nil 。

Page 59: Ruby on Rails Tutorial Part I

第二次寫的目的程式

Page 60: Ruby on Rails Tutorial Part I

第四次的測試

•終於成功了。•一次把所有資料都撈進來然後計算好像很笨耶。•我需要 Refactor 。

Page 61: Ruby on Rails Tutorial Part I

Refactor

• Red – Green - Refactor• 第一次寫的程式是為了達到目的。• 第二、三…次寫的程式是為了更好。• Unit Test 可以確保你的修改不影響結果。

Page 62: Ruby on Rails Tutorial Part I

第三次寫的目的程式

Page 63: Ruby on Rails Tutorial Part I

第五次的測試

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

Page 64: Ruby on Rails Tutorial Part I

End

謝謝大家