RailsGuidesのまとめ。
resources
resources :photos
「idをもたないアクション」・・・:index, :new, :create
「idをもつアクション」・・・:edit, :update, :show, :delete
HTTP動詞 | パス | コントローラ | #アクション | 目的 | ヘルパー |
---|---|---|---|---|---|
GET | /photos | photos | #index | 写真一覧を表示 | photos_path |
GET | /photos/new | photos | #new | 写真を1つ作成するためのHTMLフォームを返す | new_photo_path |
POST | /photos | photos | #create | 写真を1つ作成 | photos_path |
GET | /photos/:id | photos | #show | 特定の写真を表示 | photo_path(:id) |
GET | /photos/:id/edit | photos | #edit | 写真編集用のHTMLフォームを1つ返す | edit_photo_path(:id) |
PATCH/PUT | /photos/:id | photos | #update | 特定の写真を更新 | photo_path(:id) |
DELETE | /photos/:id | photos | #destroy | 特定の写真を削除 | photo_path(:id) |
resource
resource :geocoder
「パス」「createのヘルパー」が単数系になる
「indexアクション」がなくなる
「:id」がなくなる
HTTP動詞 | パス | コントローラ | #アクション | 目的 | ヘルパー |
---|---|---|---|---|---|
GET | /geocoder/new | geocoders | #new | geocoder作成用のHTMLフォームを返す | new_geocoder_path |
POST | /geocoder | geocoders | #create | geocoderを作成する | geocoder_path |
GET | /geocoder | geocoders | #show | 1つしかないgeocoderリソースを表示する | geocoder_path |
GET | /geocoder/edit | geocoders | #edit | geocoder編集用のHTMLフォームを返す | edit_geocoder_path |
PATCH/PUT | /geocoder | geocoders | #update | 1つしかないgeocoderリソースを更新する | geocoder_path |
DELETE | /geocoder | geocoders | #destroy | geocoderリソースを削除する | geocoder_path |
namespace
namespace :admin do
resources :posts, :comments
end
「パス」、「コントローラ」、「ヘルパー」がネストされる
HTTP動詞 | パス | コントローラ | #アクション | ヘルパー |
---|---|---|---|---|
GET | /admin/posts | admin/posts | #index | admin_posts_path |
GET | /admin/posts/new | admin/posts | #new | new_admin_post_path |
POST | /admin/posts | admin/posts | #create | admin_posts_path |
GET | /admin/posts/:id | admin/posts | #show | admin_post_path(:id) |
GET | /admin/posts/:id/edit | admin/posts | #edit | edit_admin_post_path(:id) |
PATCH/PUT | /admin/posts/:id | admin/posts | #update | admin_post_path(:id) |
DELETE | /admin/posts/:id | admin/posts | #destroy | admin_post_path(:id) |
(/adminが前についていない) /postsをAdmin::PostsControllerにルーティング
scope module: 'admin' do
resources :posts, :comments
end
「コントローラ」がネストされる
名前付きルートは、scopeを使用しなかった場合と同じ
HTTP動詞 | パス | コントローラ | #アクション | ヘルパー |
---|---|---|---|---|
GET | /posts | admin/posts | #index | posts_path |
GET | /posts/new | admin/posts | #new | new_post_path |
POST | /posts | admin/posts | #create | posts_path |
GET | /posts/:id | admin/posts | #show | post_path(:id) |
GET | /posts/:id/edit | admin/posts | #edit | edit_post_path(:id) |
PATCH/PUT | /posts/:id | admin/posts | #update | post_path(:id) |
DELETE | /posts/:id | admin/posts | #destroy | post_path(:id) |
/admin/postsを (Admin::なしの) PostsControllerにルーティング
scope '/admin' do
resources :posts, :comments
end
「パス」がネストされる
名前付きルートは、scopeを使用しなかった場合と同じ
HTTP動詞 | パス | コントローラ | #アクション | ヘルパー |
---|---|---|---|---|
GET | /admin/posts | posts | #index | posts_path |
GET | /admin/posts/new | posts | #new | new_post_path |
POST | /admin/posts | posts | #create | posts_path |
GET | /admin/posts/:id | posts | #show | post_path(:id) |
GET | /admin/posts/:id/edit | posts | #edit | edit_post_path(:id) |
PATCH/PUT | /admin/posts/:id | posts | #update | post_path(:id) |
DELETE | /admin/posts/:id | posts | #destroy | post_path(:id) |
ネスト
resources :magazines do
resources :ads
end
HTTP動詞 | パス | コントローラ | #アクション | 目的 |
---|---|---|---|---|
GET | /magazines/:magazine_id/ads | ads | #index | ある雑誌1冊に含まれる広告をすべて表示する |
GET | /magazines/:magazine_id/ads/new | ads | #new | ある1冊の雑誌用の広告を1つ作成するHTMLフォームを返す |
POST | /magazines/:magazine_id/ads | ads | #create | ある1冊の雑誌用の広告を1つ作成する |
GET | /magazines/:magazine_id/ads/:id | ads | #show | ある雑誌1冊に含まれる広告を1つ表示する |
GET | /magazines/:magazine_id/ads/:id/edit | ads | #edit | ある雑誌1冊に含まれる広告1つを編集するHTMLフォームを返す |
PATCH/PUT | /magazines/:magazine_id/ads/:id | ads | #update | ある雑誌1冊に含まれる広告を1つ更新する |
DELETE | /magazines/:magazine_id/ads/:id | ads | #destroy | ある雑誌1冊に含まれる広告を1つ削除する |
浅いネスト
idを持たない(:index, :new, :create)アクションだけ親のスコープの下で作成すること。
普通に書くと
resources :posts do
resources :comments, only: [:index, :new, :create]
end
resources :comments, only: [:show, :edit, :update, :destroy]
子リソースでshallowオプションを使用
resources :posts do
resources :comments, shallow: true
end
親リソースでshallowオプションを使用
resources :posts, shallow: true do
resources :comments
resources :quotes
resources :drafts
end
shallow_path
shallow_path
scope shallow_path: "sekret" do
resources :posts do
resources :comments, shallow: true
end
end
↓:shallow_pathオプションを使用すると、指定されたパラメータをパスの冒頭に追加する
HTTP | 動詞 パス | コントローラ | #アクション | 名前付きヘルパー |
---|---|---|---|---|
GET | /posts/:post_id/comments(.:format) | comments | #index | post_comments_path |
GET | /posts/:post_id/comments/new(.:format) | comments | #new | new_post_comment_path |
POST | /posts/:post_id/comments(.:format) | comments | #create | post_comments_path |
GET | /sekret/comments/:id/edit(.:format) | comments | #edit | edit_comment_path(:id) |
GET | /sekret/comments/:id(.:format) | comments | #show | comment_path(:id) |
PATCH/PUT | /sekret/comments/:id(.:format) | comments | #update | comment_path(:id) |
DELETE | /sekret/comments/:id(.:format) | comments | #destroy | comment_path(:id) |
shallow_prefix
shallow_prefix
scope shallow_prefix: "sekret" do
resources :posts do
resources :comments, shallow: true
end
end
↓:shallow_prefixオプションを使用すると、指定されたパラメータを (パスではなく) 名前付きヘルパー名の冒頭に追加する
HTTP動詞 | パス | コントローラ | #アクション | 名前付きヘルパー |
---|---|---|---|---|
GET | /posts/:post_id/comments(.:format) | comments | #index | post_comments_path |
GET | /posts/:post_id/comments/new(.:format) | comments | #new | new_post_comment_path |
POST | /posts/:post_id/comments(.:format) | comments | #create | post_comments_path |
GET | /comments/:id/edit(.:format) | comments | #edit | edit_sekret__comment_path(:id) |
GET | /comments/:id(.:format) | comments | #show | sekret__comment_path(:id) |
PATCH/PUT | /comments/:id(.:format) | comments | #update | sekret__comment_path(:id) |
DELETE | /comments/:id(.:format) | comments | #destroy | sekret__comment_path(:id) |
レストフルなアクションを追加
メンバールーティングを追加
resources :photos do
member do
get 'preview'
end
end
# ブロックを使わない書き方
resources :photos do
get 'preview', on: :member
end
HTTP動詞 | パス | コントローラ | #アクション | 名前付きヘルパー |
---|---|---|---|---|
GET | /posts/:id/preview | photos | #preview | preview_photo_path(:id) |
コレクションルーティングを追加
resources :photos do
collection do
get 'search'
end
end
# ブロックを使わない書き方
resources :photos do
get 'search', on: :collection
end
HTTP動詞 | パス | コントローラ | #アクション | 名前付きヘルパー |
---|---|---|---|---|
GET | /posts/search | photos | #search | search_photos_path |