LoginSignup
2
2
記事投稿キャンペーン 「2024年!初アウトプットをしよう」

Railsのcollectionルーティングについての言語化メモ

Last updated at Posted at 2024-01-15

Railsガイドを読んでいて、2.10.2 コレクションルーティングを追加するの部分で何をやってるのか可能な限り言語化したメモ
間違ってたら教えてください。

例として挙げられていたルーティング↓

routes.rb
resources :photos do
  collection do
    get 'search'
  end
end

これをターミナルで確認する↓

                  search_photos GET    /photos/search(.:format)                                                                          photos#search
                                  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

見てみると、photosのcollectionであるindexやらcreateの上に、何やらserchというcollection(controllerの中でいうアクション?)がGETリクエストで追加されているのがわかる。

また、この時のパスはidを伴わないものが認識されることになる。
具体的にいうと、/photos/search(.:format)の部分。
collectionと似た様なものでmemberというものがあるが、そちらはidを伴う認識がなされる。
具体的には、/photos/:id/preview(.:format) になる。

一応下記にmemberルーティングを追加した場合のroutes.rbと、その結果のターミナルの表示を残しておく。

routes.rb
resources :photos do
  member do
    get 'preview'
  end
end
                           preview_photo GET    /photos/:id/preview(.:format)                                                                     photos#preview
                                  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

呼び方がそれぞれコレクションルーティング、メンバールーティングなことについての私の現状の理解は
・コレクションルーティングはリソース全体に対する操作を伴う場合に使うので「コレクション」という名前っぽい?(indexアクションなどで使う?)
・メンバールーティングはid指定していることからも、個別のインスタンスに対する操作を想定したルーティングなので「メンバー」という名前っぽい?(showアクションなどで使う?)

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2