44
RAILS HACKATHON 起來 Rails 打造有趣的網站 Jimmy

Rails hackathon

  • Upload
    -

  • View
    314

  • Download
    0

Embed Size (px)

Citation preview

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

!

!

• rake db:migrate

使⽤用 general model ⽣生成group 的model

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

def index

def show

def new

def edit

def create

def update

def destroy

List all Groups

show a single Groups

show a new news forms

show an edit news forms

create a new news

update a news

delete a news

REST 之所以能簡化開發,是因為其所引入的架構約束。︒Rails 中的 REST implementation 將 controller 的 method 限制在七個: REpresentational State Transfer

CONTROLLER ACTION

CONTROLLER ACTIONRails Maps Actions to HTTP Methods

GET 讀取

POST 增加

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

<%= link_to (“New”,new_group_path)%>

<%= link_to (“show”,group_path(group))%>

<%= link_to (“edit”,edit_group_path(group))%>

<%= form_for @post , :url => posts_path , :html => {:method => :post} do |f| %>

CONTROLLER ACTIONHTTP 四種 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