Ruby on Rails Tutorial Part I

Post on 14-May-2015

2.866 views 3 download

Tags:

description

The Ruby on Rails Tutorial for OSDC 09。

Transcript of Ruby on Rails Tutorial Part I

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 FrameworkRails 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,

integer, 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

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

Restful - route

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

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

-RAILS_ROOT/config/routes.rb

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

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

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

謝謝大家