LoginSignup
0
0

More than 1 year has passed since last update.

【routing】collectionとmember

Posted at

collectionとmember

collectionとmemberは、ルーティングを設定する際に使用できます。
これを使用すると、生成されるルーティングのURLと実行されるコントローラーを任意にカスタマイズできます。

collection ルーティングに:idがつかない 1つのidに特定
member ルーティングにidがつく 不特定
【例】collectionで定義した場合
Rails.application.routes.draw do
  resources :tweets do
    collection do
      get 'search'
    end
  end
end
【例】collectionのルーティング
Prefix           Verb    URI                                 Pattern
search_tweets    GET    /tweets/search(.:format)              tweets#search

ルーティングに:idが付いていないことがわかります。

【例】memberで定義した場合
Rails.application.routes.draw do
  resources :tweets do
    member do
      get 'search'
    end
  end
end
【例】memberのルーティング
Prefix           Verb    URI                                 Pattern
search_tweet      GET    /tweets/:id/search(.:format)       tweets#search

URLの指定先が、collectionは:idなし、memberが:idありとなっていることが確認できます。

0
0
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
0
0