Getting Started with Rails
用Ruby on Rails純手工打造一個包含CRUD的RESTful應用程式
1. 建立一個專案
2.建立一個名為board的model,其中包含title與content兩個欄位
用Ruby on Rails純手工打造一個包含CRUD的RESTful應用程式
1. 建立一個專案
$rails new sample02
$cd sample02
$rails g model board title:string content:text
$rake db:migrate
3.建立一個名為boards的controller
$rails g controller boards
4.修改/config/routes.rb
Sample02::Application.routes.draw do
resources :boards
5.修改/config/routes.rb
Sample02::Application.routes.draw do
resources :boards
6./sample02/app/controllers/boards_controller.rb的基本結構有七組
class BoardsController < ApplicationController
def index
end
def show
end
def new
end
def create
end
def edit
end
def update
end
def destroy
end
end
7.首先要先建立boards的index與show頁,修改/sample02/app/controllers/boards_controller.rb
class BoardsController < ApplicationController
def index
@boards = Board.all
end
def show
@board = Board.find(params[:id])
end
end
8.建立views內index頁,/sample02/app/views/boards/index.html.erb
$vi ~/projects/sample02/app/views/boards/index.html.erb
9.建立views內show頁,/sample02/app/views/boards/show.html.erb
$vi ~/projects/sample02/app/views/boards/show.html.erb
10.做到這邊,我們要來驗收一下成果,透過powder來開瀏覽器驗收吧。
$cd ~/projects/sample02
$powder link
$powder open
應該可以看到這個頁面。11.完成create功能
修改/sample02/app/controllers/boards_controller.rb,加入new與create
class BoardsController < ApplicationController
def index
@boards = Board.all
end
def show
@board = Board.find(params[:id])
end
def new
@board = Board.new
end
def create
@board = Board.new(board_params)
if @board.save
redirect_to boards_path
else
render :new
end
end
private
def board_params
params.require(:board).permit(:title, :content)
end
end
/sample02/app/views/boards/new.html.erb
$vi ~/projects/sample02/app/views/boards/new.html.erb
12.完成edit功能
修改/sample02/app/controllers/boards_controller.rb,加入edit與update
class BoardsController < ApplicationController
def index
@boards = Board.all
end
def show
@board = Board.find(params[:id])
end
def new
@board = Board.new
end
def create
@board = Board.new(board_params)
if @board.save
redirect_to boards_path
else
render :new
end
end
def edit
@board = Board.find(params[:id])
end
def update
@board = Board.find(params[:id])
if @board.update(board_params)
redirect_to board_path(@board)
else
format.html { render :action => "edit" }
end
end
private
def board_params
params.require(:board).permit(:title, :content)
end
end
/sample02/app/views/boards/edit.html.erb
$vi ~/projects/sample02/app/views/boards/edit.html.erb
13.完成destroy功能
修改/sample02/app/controllers/boards_controller.rb,加入destroy
class BoardsController < ApplicationController
def index
@boards = Board.all
end
def show
@board = Board.find(params[:id])
end
def new
@board = Board.new
end
def create
@board = Board.new(board_params)
if @board.save
redirect_to boards_path
else
render :new
end
end
def edit
@board = Board.find(params[:id])
end
def update
@board = Board.find(params[:id])
if @board.update(board_params)
redirect_to board_path(@board)
else
format.html { render :action => "edit" }
end
end
def destroy
@board = Board.find(params[:id])
@board.destroy
redirect_to boards_path
end
private
def board_params
params.require(:board).permit(:title, :content)
end
end
完整的boards_controller.rb
14.完成CRUD功能囉,來驗收一下吧。
在瀏覽器上打上http://sample02.dev/boards/
顯示某一筆資料的show功能
新增資料的new功能
編輯資料的edit功能
刪除資料的destroy功能
留言
張貼留言