50
使用 Ruby on Rails 快速打造你的 Web APP http://goo.gl/Z7ID5I

Ksdg 使用 ruby on rails 快速打造你的 web app

Embed Size (px)

DESCRIPTION

Introduce ruby on rails

Citation preview

使用 Ruby on Rails快速打造你的 Web APP

http://goo.gl/Z7ID5I

自我介紹

Eddie Li米邦國際資訊有限公司 Co-founder

PHP / ZF1 / Web Security / Web Crawler / Ruby on Rails

Blog: adz.visionbundles.comFB: latebird.ticketEmail: [email protected]

先說好! 我只摸 RoR 四個月

有講錯的地方請多多包含!

Rails 精神 (一)

Convension Over Configuration

Rails 精神 (二)

DRY (Don’t Repeat Yourself)

開始講 Rails 之前

Ruby Syntax

Ruby String & Symbol

Block

聽說 Rails 很先進以前寫 PHP 的時候很懷疑這句話

Rails 的預設值

1. 使用 coffeescript2. 使用 sass / scss3. pjax 套件 turbolinks4. assets pipline5. scaffold (今天要分享的主題)

Bundler

Gem - 幾乎無縫整合的套件, Library, Helper, Tools

Bundler - 管理 Gem 相依的工具

找 Gem 的方法請發揮你的想像力

www.ruby-toolbox.comrubygems.org

開啟一個新專案

Scaffold一個指令搞定 CRUD

這麼簡單一定有鬼

難拆開解釋的 MVC 架構

1. 建好 Migration2. 建好 Model 3. 建好 RESTful style router 到 controller 4. 建好一個 controller + 6 個 actions5. 建好 View

a. index.html.erb - 清單

b. edit.html.erb - 編輯頁面

c. new.html.erb - 新增頁面

… 等等 7. 建好相關 assets file (coffee, scss)8. 其他

Scaffold 幫你做的

一. 把 db schema 納入 code 的版本管理

二. 慣例以 id 當 primary key三. t.timestamp 自動加上 column

a. updated_at => datetimeb. created_at => datetime

git checkout 回任何一個版本 schema 都還在 :)

Migration 1

1. rake db:create2. rake db:migrate

實際的 schema 長這樣:

Migration 2

多一張表 schema_migrations

1. Migration File 以 timestamp 命名

2. 多人開發時,不會因為某個小白改到 schema3. 隨時可以 rake db:rollback 回到上一個 schema 版本

Migration 3

注意事項:a. 非常小心使用

b. commit migration 前,先試試 migrate & rollback 都正常

c. 若沒有使用正常流程可能會有 migrate conflict

Migration 4

Router 定義 resource 後依照 REST Style URL1. 自動對應到 controller 內 7 個 actions2. 自動產生相關的 URL Helper 可以在 View 中使用

Router (Resource)

需要依附在某個 resource 底下的 resource (yo!)

例如: 一個 template 有多個 pages

URL 長這樣:

Router (Nested Resource)

可以這樣用

剛剛定義的 resource 會產生 URL HTLPER

Router (URL Helper 1)

Router (URL Helper 2)

應用場景: 1 to 1 關聯

(如一個 user 關聯到一個 profile)

Router (Single Resource)

Model (無敵好用的 ORM)

用 Model 撈資料

Model (用物件方式存取 db 資料)

Table Column 宣告成 Attribute & Dirty Objects (instance method)

Model (更新資料)

更新資料一樣很簡單

P.S. 若 model 內有 updated_at or created_at 也會一併更新

Model (關聯範例)

Model (還不止這樣)

scope - 把 query 拆成有意義的 function,增加 readable & maintainablemixin - 把共用、複雜的 model 拆出來,達到優雅的程式碼 + DRY validator - 把資料驗證邏輯寫在 Model 跟 Form (等等會提)serialize - 讓你的 RDBMS 有一咪咪 Free schema 的感覺

callbacks - 提供 after_create, before_save, ….. 多個 callback

….. 講完可能就明天了

Controller (index) 1/7

Controller (show, new, edit) 4/7

Controller (Create) 5/7

Controller (Update, Destroy) 7/7

View 只是一種表現方式隨時可以抽換

DB => Model => 商業邏輯 => Controller => View

1. HTML2. Excel3. Dynamic Javascript4. JSON5. XML

Views 可以是

依照 router 慣例,resource 綁定 URL helper

View Helper (form_for)

除了有 form_for 的功能外還有:

1. form template - 提供熱門套件 bootstrap … etc2. 綁定 input type - 依照 db column data type3. 其他瑣碎的事 - i18n, hints, label, error message display

更強的 [gem] simple_form

如果再搭配 [gem] client_side_validations

還可直接把 model 裡的 validate 一次搬來前端

DRY Again

示範一下

用 scaffold 完成一個 ….

登入認證用的 Gem

1. Devise - 10 分鐘完成你的會員系統

2. omniauth - 提供統一規格界面實作多方認證

omniauth-facebookomniauth-twitteromniauth-github

omniauth-renren omniauth-weibo

有趣的 Gem

1. capybara-webkit - Ruby Webkit Driver

2. faye-websocket - Web Socket Server

3. rubcas-sever - cas protocol 實作的 SSO Server

Ruby / RoR 相關的 Project

1. Gitlab - self hosting github2. Redmine - project management tool3. Selfstarter - crowd-founding

參考至: http://www.opensourcerails.com/

Rails 相關 SaaS

1. Code Climate - code review 工具

2. New Relic - server & app monitor

其他關於 RoR 的事情

安全性、社群文化、缺點

Q & A有請各位提問 !!!!!!!!!!

我們需要高手相助

米邦國際資訊有限公司 VisionBundles Int’l Ltd.晚鳥票 / EC 平台 / 網頁遊戲開發

Senior Ruby on Rails Web Developer (一名)Senior F2E (一名)

歡迎您來參加

Group: Kaohsiung Rails Meetup x 讀書會

http://goo.gl/VkkZAb