Help us understand the problem. What is going on with this article?

rails routes.rbのmemberとcollectionの違い

routes.rbのmemberとcollectionの違い

railsのroutes.rbでルーティングを設定する時にmemberとcollectionの違いに関して記載します。

menberの場合

routes.rb
resources :buy_additional_actions, only: %i[] do
   member do
      get 'index' => 'buy_additional_actions#index'

menberの場合は生成されたurlに:idが自動で追加されます。

buy_additional_action GET /buy_additional_actions/:id/index(.:format) buy_additional_actions#index

collectionの場合

routes.rb
resources :buy_additional_actions, only: %i[] do
   collection do
      get 'index' => 'buy_additional_actions#index'

collectionの場合はurlには:idが付与されません。

buy_additional_actions GET /buy_additional_actions/index(.:format) buy_additional_actions#index
assign-navi
株式会社アサインナビは、日本最大級のエンジニア・IT企業とIT案件のマッチングサイト「アサインナビ」を運営する企業です。
https://assign-navi.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away