memberとcollectionオプション(ルーティング)

resourcesを用いると7つのルーティングが一度に作成される。memberとcollectionオプションはどちらも新たにルーティングを加えたい場合に用いる。

member…特定のデータ(:idが入る)に対するアクションを追加する際に用いる。

collection…ユーザー検索や商品検索等、全てのデータを対象とするアクションに対し用いる。


resources :photos do collection do get 'search' end end

resources :photos do get 'search', on: :collection end

on: :collectionという書き方も可能。


resources :photos do get 'preview', on: :member end

memberについてもon: :memberの書き方が可能。

Rails のルーティング - Railsガイド

DIVE INTO CODE | Railsのルーティングを学ぼう②