Ruby on Rails 網站程式設計基礎班

Lesson 8

Quiz 3

ActiveRecord Validation

  • 確保從前端回傳的表單資料正確性
  • 寫在 model 檔案裡
class User < ActiveRecord::Base
  validates :name, presence: true
  validates :name, length: { minimum: 2}
end

table 命名法

  • 有些時候我們不確定 table 的命名法
  • 進入 rails console
  • 使用 .tableize 方法
irb(main):001:0> "User".tableize
=> "users"
irb(main):002:0> "Person".tableize
=> "people"
irb(main):003:0> "Category".tableize
=> "categories"
irb(main):004:0> "CrazyMonkey"
=> "CrazyMonkey"

Check Routes

  • 需要檢查我們在 routes.rb 裡面設定的路由是否正確
  • 到瀏覽器輸入 /rails/info
  • 在 terminal 輸入 rake routes

用 Bootstrap 來美化我們的 view

  • 有名的前端 library
  • 適合結省開發者的時間
1. 到 gemfile 裡面加入 gem 'bootstrap-sass'

2. bundle install

3. 在 app/assets/stylesheets/application.css 裡輸入 @import "bootstrap"; 

4. 在 app/assets/javascript/application.js 裡輸入 //= require bootstrap

asset Pipeline

  • 把所有的靜態資源(javascirpt, css檔) 壓縮成一個檔案
  • 減少瀏覽器Request下載次數
  • 加速下載時間

實作留言板

  • 想好 Schema
  • 寫 migration 檔案把資料表建好
  • 設定好 model 之間的關聯

功課

deck

By Eugene Chang