Jex’s Note

Rails Route

command

  • rake routes : 查看所有routes
  • rake routes | grep user : 查看所有 routes 含有 user 關鍵字的

觀念

4 HTTP methods, 4 URL helper, 7 actions

Helper                      GET                 POST            PUT 或 PATCH    DELETE
event_path(@event)          /events/1                           /events/1       /events/1
                            show                                update          destroy

events_path                 /events             /events
                            index               create

edit_event_path(@event)     /events/1/edit
                            edit

new_event_path              /events/new
                            new
  • show, edit, update, destroy 是單數, 對特定元素操作
  • index, create 是複數, 對群集操作

基本 Route 語法

root 'welcome#index'

# 7 個 action 都使用
resources :info do

    # 只允許 edit, update
    resources :users, only: [:edit, :update]

    # 除了 show 其他 action 都用
    resources :products, except: [:show]

    # 指定使用 PUT
    put 'change_password', on: :member
end

# namespace 是 folder 名稱
namespace :dashboard  do
  # 原本 dashboard 在 route 的命名改為 admin, 通常是為了美化 route 或是減少不直覺的 url 造成的困惑才會使用
  resources :welcome, as: 'home'
end

# 兩者是一樣的
resources :search, only: [:show]
get '/search/:id', to: 'search#show', as: 'search'      # to: action, as: route name

Difference between member and collection

如果要帶上原本物件的 id 就用 :member

如果只是需要一個一般的頁命就用 :collection

i.e.

resources :posts do
  # on collection
  get 'search', on: :collection             # '/posts/search' and search_posts_path

  # on member
  get 'share', on: :member                  # '/posts/:id/share' and share_photo_path(@post)
end

member 與 collection 那一種寫法

resources :users do
  member do
    get :find
  end

  collection do
    get  :find
    post :freeze
  end
end

Url / Path 相關

完整 url

request.original_url
> http://127.0.0.1:3000/dashboard/admin/110/find_name

Path

request.path (= request.full.path)
> /dashboard/admin/110/find_name

判斷目前 path 是否一樣

current_page?(new_product_path)
> True / False

將 hash 轉為 query string

{ name: 'David', nationality: 'Danish' }.to_query
> name=David&nationality=Danish

在 Rails consloe 下

使用 route path 必須要先引入

include Rails.application.routes.url_helpers
> root_path

印出所有的 assets path

Rails.application.config.assets.paths

# 條列式
y Rails.application.config.assets.paths
ap Rails.application.config.assets.paths

path helper

xxxx_path(anchor: 'xx')             # /xxxx#xx
xxxx_path(format: :json)            # /xxxx.json

Dashboard 設計

config/route.rb :

root 'welcome#index'                    # 首頁

namespace :dashboard do
  root 'welcome#index'                  # dashboard 的首頁
  resources :musics, only: [:index]
end

controllers/dashboard_controller.rb

class DashboardController < ApplicationController
end

controllers/dashboard/welcome_controller.rb :

class Dashboard::WelcomeController < DashboardController
  def index
  end
end

views/dashboard/welcome/index.rb :

dashboard index

另一種寫法 dashboard_controller.rb 放在 controllers/dashboard 下

controllers/dashboard/dashboard_controller.rb

class Dashboard::DashboardController < ApplicationController

controllers/dashboard/welcome_controller.rb :

class Dashboard::WelcomeController < Dashboard::DashboardController

Comments