GET /patients/17 のリクエスト -> ルートテーブルをチェック -> マッチングルート get '/patients/:id', to: 'patients#show'params に { id: '17' } として、patients コントローラーの show アクションに渡されるget '/patients/:id', to: 'patients#show' -> get '/patients/:id', to: 'patients#show', as: 'patient' に修正可能@patient = Patient.find(17) としてインスタンス変数を宣言patient_path(@patient) を使用 -> 自動的に /patients/17 のパスを生成、ルーターヘルパーでIDの指定は不要以下のようなリクエストをRailsが受ける
DELETE /photos/17
Railsはこのリクエストをコントローラーアクションにマッピングするためにルーターを要求する
この時、最初にマッチングされるルートが
resources :photos
であれば
Railsは params 内に { id: '17' } を入れて
photos コントローラーの destroy メソッドに送る。
| HTTP Verb | Path | Controller#Action | Used for |
|---|---|---|---|
| GET | /patients | patients#index | 全患者のリストを表示するページのパス |
| GET | /patients/new | patients#new | 新しい患者を作成するフォームのページのパス |
| POST | /patients | patients#create | 新しい患者を作成する |
| GET | /patients/:id | patients#show | 特定の患者を表示する |
| GET | /patients/:id/edit | patients#edit | 特定の患者を編集するフォームのページのパス |
| PATCH/PUT | /patients/:id | patients#update | 特定の患者を更新する |
| DELETE | /patients/:id | patients#destroy | 特定の患者を削除する |
| Helper Function | URL Pattern | Description |
|---|
| photos_path | /photos | 全ての写真のリストを表示するページのパス |
| new_photo_path | /photos/new | 新しい写真を作るフォームがあるページのパス |
| edit_photo_path | /photos/:id/edit | 特定の写真を編集するフォームがあるページのパス。例: edit_photo_path(10) は /photos/10/edit を返す |
| photo_path | /photos/:id | 特定の写真の詳細ページのパス。例: photo_path(10) は /photos/10 を返す |
resources :photos, :books, :videos
上記のコードと
resources :photos
resources :books
resources :videos
このコードは同じです。