SlideShare a Scribd company logo
1 of 44
Download to read offline
RAILS HACKATHON
⼀一起來⽤用 Rails 打造有趣的網站

Jimmy
學習⺫⽬目標
•

學習 Rails CRUD 基本功	


•

Controller, model 概念	


•

Routing 設定
WHY RAILS
• MVC
• RESTFUL
• USE BUNDLER
• DEFAULT
• SUPPORT SQLITE
• EASY TO TEST

最⽕火紅的語⾔言之⼀一
開始吧
NEW PROJECT
•

rails new news
GENERAL MODEL
•

rails g model group title:string description:text 	

!

!
使⽤用 general model ⽣生成group 的model

•

rake db:migrate
GENERAL CONTROLLER
•

rails g controller groups
修改 ROUTE
打開 route.rb	

• 加⼊入 resource : groups
•
修改 CONTROLLER
打開 groups_controller.rb	

• 加⼊入 class method
•
加⼊入 INDEX VIEW
•

app/views/groups 下新增 index.html.erb
啟動 SERVER
•

rails s -p 8888

預設 port 3000
OPEN BROWSER
•

http://localhost:8888/groups
MODEL 說明
GROUPS MODEL
•

⾃自動加⼊入 id , create_at , update_at
RAILS CONSOLE
•

rails c

The console command lets you interact with your Rails application from the
command line. On the underside, rails console uses IRB, so if you've ever used it,
you'll be right at home. This is useful for testing out quick ideas with code and
changing data server-side without touching the website.
使⽤用CONSOLE 新增資料
•

Group.create	

!
!
!

•

Group.new

g = Group.new
g.title=‘t2’
g.description= ‘t2 description’
g.save
CONTROLLER 說明
CONTROLLER ACTION
REST 之所以能簡化開發,是因為其所引入的架構約束。︒Rails 中的
REST implementation 將 controller 的 method 限制在七個:	

REpresentational State Transfer
def index

List all Groups

def show

show a single Groups

def new

show a new news forms

def edit

show an edit news forms

def create

create a new news

def update

update a news

def destroy

delete a news
CONTROLLER ACTION
CONTROLLER ACTION
Rails Maps Actions to HTTP Methods	

<%= link_to (“List”,groups_path)%>

GET	

讀取

<%= link_to (“New”,new_group_path)%>
<%= link_to (“show”,group_path(group))%>
<%= link_to (“edit”,edit_group_path(group))%>

POST	

增加

<%= form_for @post , :url =>
posts_path , :html => {:method
=> :post} do |f| %>
CONTROLLER ACTION
HTTP 四種 verb.	


PUT	

更新

DELETE	

刪除

<%= form_for @post , :url =>
post_path(@post) , :html => {:method
=> :put} do | f |%>

<%= link_to("Destroy",
post_path(@post), :method
=> :delete )
CONTROLLER ACTION
•

define index action
CONTROLLER ACTION
•

define index method

預設 render page 為 def 的名稱	

指定 page 使⽤用 如下
新增功能

Create
建⽴立增新畫⾯面
•

修改 index.html.erb
link_to “new", new_group_path
等同 html <a href=“/groups/new“>new</a>

link_to “new", new_group_path, id: “newid", class: “btn"

等同 html <a href=“/groups/new“ class=“btn“ id=“newid“>new</a>
建⽴立增新畫⾯面
•

修改 Controller
在GroupsController 裡加⼊入 new 的 action
建⽴立增新畫⾯面
•

增加 new.html.erb
建⽴立CREATE ACTION
•

在 Controller 中增加⼀一個 create 的 action
MODEL VALIDATE
•

在 Model 中增加檢核條件	


!
!
!

•
!

在表單增加 error message
修改功能

Update
建⽴立修改畫⾯面
•

修改 index.html.erb
link_to “修改", edit_group_path
等同 html <a href=“/groups/1/edit“>修改</a>
建⽴立UPDATE ACTION
•

修改 Controller
建⽴立VIEW
•

新增 edit view
刪除功能

Delete
建⽴立修改畫⾯面
•

修改 index.html.erb
指定 method 為 delete
建⽴立UPDATE ACTION
•

修改 Controller
ROUTING
資料來源:rails 101
RAILS ROUTING SETTING
config/route.rb 檔設定 mapping rule	

• 常⾒見幾種寫法
•

get “posts/new"
resources :posts
root :to => “posts#index”
match “/posts” => “posts#index”, as =>“posts“
RAILS ROUTING SETTING
resources :posts do	

	

 resouces :comments	

end

namespace :admin do	

	

 resouces :posts	

end
資料來源:rails 101
THE END

More Related Content

Similar to Rails hackathon

View 與 Blade 樣板引擎
View 與 Blade 樣板引擎View 與 Blade 樣板引擎
View 與 Blade 樣板引擎Shengyou Fan
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xBo-Yi Wu
 
View 與 Blade 樣板引擎
View 與 Blade 樣板引擎View 與 Blade 樣板引擎
View 與 Blade 樣板引擎Shengyou Fan
 
Schema & Migration操作
Schema & Migration操作Schema & Migration操作
Schema & Migration操作Shengyou Fan
 
Asp.net mvc 培训
Asp.net mvc 培训Asp.net mvc 培训
Asp.net mvc 培训lotusprince
 
HTML5概览
HTML5概览HTML5概览
HTML5概览Adam Lu
 
View 與 Blade 樣板引擎
View 與 Blade 樣板引擎View 與 Blade 樣板引擎
View 與 Blade 樣板引擎Shengyou Fan
 
使用 ASP.NET 5 實戰開發雲端應用程式
使用 ASP.NET 5 實戰開發雲端應用程式使用 ASP.NET 5 實戰開發雲端應用程式
使用 ASP.NET 5 實戰開發雲端應用程式Will Huang
 
Spring mvc
Spring mvcSpring mvc
Spring mvcpepsixp
 
Rest Ruby On Rails
Rest Ruby On RailsRest Ruby On Rails
Rest Ruby On Railsshaokun
 
Underscore
UnderscoreUnderscore
Underscorecazhfe
 
Template mb-kao
Template mb-kaoTemplate mb-kao
Template mb-kaoxwcoder
 
Mongo db introduction
Mongo db introductionMongo db introduction
Mongo db introductionDai Qifeng
 
KISSY Editor Design 2
KISSY Editor Design 2KISSY Editor Design 2
KISSY Editor Design 2yiming he
 
4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdf4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdfssuserd6c7621
 
使用Big pipe提升浏览速度 wk_velocity
使用Big pipe提升浏览速度 wk_velocity使用Big pipe提升浏览速度 wk_velocity
使用Big pipe提升浏览速度 wk_velocitykumawu
 
ASP.Net WebAPI經驗分享
ASP.Net WebAPI經驗分享ASP.Net WebAPI經驗分享
ASP.Net WebAPI經驗分享國昭 張
 
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器Chieh Kai Yang
 

Similar to Rails hackathon (20)

View 與 Blade 樣板引擎
View 與 Blade 樣板引擎View 與 Blade 樣板引擎
View 與 Blade 樣板引擎
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.x
 
View 與 Blade 樣板引擎
View 與 Blade 樣板引擎View 與 Blade 樣板引擎
View 與 Blade 樣板引擎
 
Schema & Migration操作
Schema & Migration操作Schema & Migration操作
Schema & Migration操作
 
Asp.net mvc 培训
Asp.net mvc 培训Asp.net mvc 培训
Asp.net mvc 培训
 
HTML5概览
HTML5概览HTML5概览
HTML5概览
 
View 與 Blade 樣板引擎
View 與 Blade 樣板引擎View 與 Blade 樣板引擎
View 與 Blade 樣板引擎
 
Hadoop hive
Hadoop hiveHadoop hive
Hadoop hive
 
敦群學院-SharePoint精英計畫-系統開發-Day 2
敦群學院-SharePoint精英計畫-系統開發-Day 2敦群學院-SharePoint精英計畫-系統開發-Day 2
敦群學院-SharePoint精英計畫-系統開發-Day 2
 
使用 ASP.NET 5 實戰開發雲端應用程式
使用 ASP.NET 5 實戰開發雲端應用程式使用 ASP.NET 5 實戰開發雲端應用程式
使用 ASP.NET 5 實戰開發雲端應用程式
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Rest Ruby On Rails
Rest Ruby On RailsRest Ruby On Rails
Rest Ruby On Rails
 
Underscore
UnderscoreUnderscore
Underscore
 
Template mb-kao
Template mb-kaoTemplate mb-kao
Template mb-kao
 
Mongo db introduction
Mongo db introductionMongo db introduction
Mongo db introduction
 
KISSY Editor Design 2
KISSY Editor Design 2KISSY Editor Design 2
KISSY Editor Design 2
 
4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdf4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdf
 
使用Big pipe提升浏览速度 wk_velocity
使用Big pipe提升浏览速度 wk_velocity使用Big pipe提升浏览速度 wk_velocity
使用Big pipe提升浏览速度 wk_velocity
 
ASP.Net WebAPI經驗分享
ASP.Net WebAPI經驗分享ASP.Net WebAPI經驗分享
ASP.Net WebAPI經驗分享
 
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
 

Rails hackathon