- Ruby on Rails 教程
- Ruby on Rails - 首頁
- Ruby on Rails - 簡介
- Ruby on Rails - 安裝
- Ruby on Rails - 框架
- Ruby on Rails - 目錄結構
- Ruby on Rails - 示例
- Ruby on Rails - 資料庫設定
- Ruby on Rails - Active Records
- Ruby on Rails - 遷移
- Ruby on Rails - 控制器
- Ruby on Rails - 路由
- Ruby on Rails - 檢視
- Ruby on Rails - 佈局
- Ruby on Rails - 腳手架
- Ruby on Rails - AJAX
- Ruby on Rails - 檔案上傳
- Ruby on Rails - 傳送郵件
- Ruby on Rails 資源
- Ruby on Rails - 參考指南
- Ruby on Rails - 快速指南
- Ruby on Rails - 資源
- Ruby on Rails - 討論
- Ruby 教程
- Ruby 教程
Ruby on Rails - 腳手架
在開發 Rails 應用程式時,特別是那些主要提供資料庫中資料簡單介面的應用程式,使用腳手架方法通常很有用。
腳手架提供的不僅僅是廉價的演示效果。以下是一些好處:
您可以快速向用戶提供程式碼以獲取反饋。
您會被更快的成功所激勵。
您可以透過檢視生成的程式碼來了解 Rails 的工作原理。
您可以使用腳手架作為基礎來啟動您的開發。
腳手架示例
為了理解腳手架,讓我們建立一個名為cookbook的資料庫和一個名為recipes的表。
建立空的 Rails Web 應用程式
開啟命令視窗並導航到您要建立此cookbook Web 應用程式的位置。因此,執行以下命令以建立完整的目錄結構。
tp> rails new cookbook
設定資料庫
以下是建立資料庫的方法:
mysql> create database cookbook; Query OK, 1 row affected (0.01 sec) mysql> grant all privileges on cookbook.* to 'root'@'localhost' identified by 'password'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
要指示 Rails 如何查詢資料庫,請編輯配置檔案 cookbook\config\database.yml 並將資料庫名稱更改為 cookbook。密碼留空。完成後,它應如下所示:
development: adapter: mysql database: cookbook username: root password: [password] host: localhost test: adapter: mysql database: cookbook username: root password: [password] host: localhost production: adapter: mysql database: cookbook username: root password: [password] host: localhost
Rails 允許您在開發模式、測試模式或生產模式下執行,使用不同的資料庫。此應用程式對每個模式都使用相同的資料庫。
生成的腳手架程式碼
使用腳手架操作,Rails 會動態生成它所需的所有程式碼。透過執行scaffold作為指令碼,我們可以將所有程式碼寫入磁碟,在那裡我們可以檢查它,然後開始根據我們的需求對其進行調整。
現在,讓我們再次開始使用腳手架輔助指令碼手動生成腳手架程式碼:
cookbook> rails generate scaffold recipe
它生成如下所示的自動檔案:
控制器
讓我們看一下控制器背後的程式碼。此程式碼由scaffold生成器生成。如果您開啟 app/controllers/recipes_controller.rb,那麼您會發現如下內容:
class RecipesController < ApplicationController
before_action :set_recipe, only: [:show, :edit, :update, :destroy]
# GET /recipes
# GET /recipes.json
def index
@recipes = Recipe.all
end
# GET /recipes/1
# GET /recipes/1.json
def show
end
# GET /recipes/new
def new
@recipe = Recipe.new
end
# GET /recipes/1/edit
def edit
end
# POST /recipes
# POST /recipes.json
def create
@recipe = Recipe.new(recipe_params)
respond_to do |format|
if @recipe.save
format.html { redirect_to @recipe, notice: 'Recipe was successfully created.' }
format.json { render :show, status: :created, location: @recipe }
else
format.html { render :new }
format.json { render json: @recipe.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /recipes/1
# PATCH/PUT /recipes/1.json
def update
respond_to do |format|
if @recipe.update(recipe_params)
format.html { redirect_to @recipe, notice: 'Recipe was successfully updated.' }
format.json { render :show, status: :ok, location: @recipe }
else
format.html { render :edit }
format.json { render json: @recipe.errors, status: :unprocessable_entity }
end
end
end
# DELETE /recipes/1
# DELETE /recipes/1.json
def destroy
@recipe.destroy
respond_to do |format|
format.html { redirect_to recipes_url, notice: 'Recipe was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_recipe
@recipe = Recipe.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def recipe_params
params.require(:recipe).permit(:tittle, :instructions)
end
end
當 Rails 應用程式的使用者選擇一個操作(例如“顯示”)時,控制器將執行相應部分中的任何程式碼(“def show”),然後預設情況下將呈現同名的模板(“show.html.erb”)。此預設行為可以被覆蓋。
控制器使用 ActiveRecord 方法(如find、find_all、new、save、update_attributes 和 destroy)將資料移入和移出資料庫表。請注意,您不必編寫任何 SQL 語句,Rails 將自動處理它。
這單行程式碼將使資料庫表活躍起來。它將提供一個簡單的資料庫介面,以及以下方法:
- 建立新條目
- 編輯當前條目
- 檢視當前條目
- 刪除當前條目
在建立或編輯條目時,腳手架將為您完成所有繁重的工作,例如表單生成和處理,甚至提供智慧表單生成,支援以下型別的輸入:
- 簡單的文字字串
- 文字區域(或大塊文字)
- 日期選擇器
- 日期時間選擇器
您可以使用 Rails 遷移來建立和維護表。
rake db:migrate RAILS_ENV=development
現在,轉到 cookbook 目錄並使用以下命令執行 Web 伺服器:
cookbook> rails server
現在,開啟瀏覽器並導航到http://127.0.0.1:3000/recipe/new。這將為您提供一個螢幕,用於在 recipes 表中建立新條目。螢幕截圖如下所示:
一旦您按下建立按鈕來建立一個新的食譜,您的記錄就會新增到 recipes 表中,並顯示以下結果:
您可以看到編輯、顯示和刪除記錄的選項。因此,您可以試用這些選項。
您還可以使用 URL http://127.0.0.1:3000/recipe/list 列出 recipes 表中所有可用的食譜。
增強模型
Rails 為您提供了大量的錯誤處理。要理解這一點,請向空的 recipe 模型新增一些驗證規則:
修改 app/models/recipe.rb 如下,然後測試您的應用程式:
class Recipe < ActiveRecord::Base validates_length_of :title, :within => 1..20 validates_uniqueness_of :title, :message => "already exists" end
這些條目將進行自動檢查。
validates_length_of - 欄位不為空且不太長。
validates_uniqueness_of - 重複值會被捕獲。在這裡,我們給出了自定義訊息,而不是預設的 Rails 錯誤訊息。
建立腳手架的替代方法
如上所示建立應用程式,並使用如下所示的生成的腳手架程式碼
rails g scaffold Recipe tittle:string instructions:text
上面的程式碼使用 sqlite3 生成包含資料庫的自動檔案,標題和說明列如下圖所示。
我們需要使用以下語法遷移資料庫。
$ rake db:migrate RAILS_ENV=development
最後,使用以下命令列執行應用程式:
rails server
它將生成如上所示的輸出影像結果。
檢視
所有檢視和相應的控制器方法都是由scaffold命令建立的,它們位於 app/views/recipes 目錄中。
腳手架有何不同?
如果您已經閱讀了前面的章節,那麼您一定已經看到我們建立了列出、顯示、刪除和建立資料等方法,但腳手架會自動完成這項工作。