iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 20
1
Modern Web

Ruby礦工的Rails地圖系列 第 20

路由 routes

在本日截稿的最後半小時
決定來談一下路由
這是我開始寫rails才接觸的觀念
一開始也吃了很多悶虧
熟悉以後,覺得真的很方便(也不算是真正很熟悉啦)

最簡單的講法:
路由就是決定網址到controller的對應關係
不同的網址可以到相同的controller
了解路由可以說是rails的基礎

實務上,九成的路由都是像下面這樣

resources :photos

由resources加上model name所組成
resources會自動加上8種方法
分別是

Prefix Verb   URI Pattern                Controller#Action
    photos GET    /photos(.:format)          photos#index
           POST   /photos(.:format)          photos#create
 new_photo GET    /photos/new(.:format)      photos#new
edit_photo GET    /photos/:id/edit(.:format) photos#edit
     photo GET    /photos/:id(.:format)      photos#show
           PATCH  /photos/:id(.:format)      photos#update
           PUT    /photos/:id(.:format)      photos#update
           DELETE /photos/:id(.:format)      photos#destroy

最常用的就是create,update,show,index,destroy
如果其中有幾種你確定用不上,可以加上限制

resources :photos, only: :index

這樣就只會生成index
反之,可以用except方法

resources :photos, except: [:new,:edit]

如果規則很單純,也可一次定義多個model

resources :photos, :books, :videos

如果想要建立的路由不需要ID也可以查詢
也就是單一的資源
可以用resource方法(不加s)
如此會產生沒有id的單數方法

resource :photos

產生的路由如下:

    Prefix Verb   URI Pattern            Controller#Action
     photos POST   /photos(.:format)      photos#create
 new_photos GET    /photos/new(.:format)  photos#new
edit_photos GET    /photos/edit(.:format) photos#edit
            GET    /photos(.:format)      photos#show
            PATCH  /photos(.:format)      photos#update
            PUT    /photos(.:format)      photos#update
            DELETE /photos(.:format)      photos#destroy

可以與上面複數所產生的做對照
很明顯單沒有id參數了

一般的專案會有前後台
而後台的操作,通常會如下

namespace :admin do
  resources :posts
end

產生的路由如下:

    admin_posts GET    /admin/posts(.:format)          admin/posts#index
                POST   /admin/posts(.:format)          admin/posts#create
 new_admin_post GET    /admin/posts/new(.:format)      admin/posts#new
edit_admin_post GET    /admin/posts/:id/edit(.:format) admin/posts#edit
     admin_post GET    /admin/posts/:id(.:format)      admin/posts#show
                PATCH  /admin/posts/:id(.:format)      admin/posts#update
                PUT    /admin/posts/:id(.:format)      admin/posts#update
                DELETE /admin/posts/:id(.:format)      admin/posts#destroy

前面就會有admin前綴
這樣的目的是在整理controller時
在後台(admin)的controller都可以在同一個資料夾內
路徑上也有明顯的區別
但如果只是controller想在admin下
但URL不想動,可以用下面的寫法

scope module: 'admin' do
  resources :posts
end
#如果只有一行也可以這樣寫
resources :posts, module: 'admin'

觀察url的部分,就會少了admin

    posts GET    /posts(.:format)          admin/posts#index
          POST   /posts(.:format)          admin/posts#create
 new_post GET    /posts/new(.:format)      admin/posts#new
edit_post GET    /posts/:id/edit(.:format) admin/posts#edit
     post GET    /posts/:id(.:format)      admin/posts#show
          PATCH  /posts/:id(.:format)      admin/posts#update
          PUT    /posts/:id(.:format)      admin/posts#update
          DELETE /posts/:id(.:format)      admin/posts#destroy

與剛剛的需求相反
如果是想要保留URL,但是controller不要有admin
作法如下

scope '/admin' do
  resources :posts
end
#也可以這樣寫
resources :posts, path: '/admin/posts'
#產出如下
    posts GET    /admin/posts(.:format)          posts#index
          POST   /admin/posts(.:format)          posts#create
 new_post GET    /admin/posts/new(.:format)      posts#new
edit_post GET    /admin/posts/:id/edit(.:format) posts#edit
     post GET    /admin/posts/:id(.:format)      posts#show
          PATCH  /admin/posts/:id(.:format)      posts#update
          PUT    /admin/posts/:id(.:format)      posts#update
          DELETE /admin/posts/:id(.:format)      posts#destroy

剛好與上方相反
是不是很神奇呀!!

今天就先介紹到這邊
大家聖誕快樂


上一篇
How to set command line alias on mac
下一篇
路由 routes (2)
系列文
Ruby礦工的Rails地圖30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言