LoginSignup
0
0

More than 1 year has passed since last update.

ネスト、collectionとmemberの違いについて(ルーテイング設定)

Posted at

ルーテイング設定のネスト、collectionとmemberの違いについて学んでいきましょう。

resources :stores do
  resources :products
  member do
    get 'drink'
  end
  collection do
    get 'ice'
  end
end

まずはネストから学んでいきましょう。
記載方法としては上の1~2行目です。
このようにresourcesの中にresourcesを記載することで
親子関係を作ることができます。
routingがこのようになります。

#ネストしたルーテイング
/stores/:store_id/products/:id

storeの中にproductが入り込んでいるイメージです。

次は一気にmemberとcollectionを見ていきましょう。
一番最初のコードの2行目以外にご注目ください。
このmemberとcollectionを使うと下のようにルーテイングが変化します。
ルーテイングが変わることによって、基本のアクション以外のルーテイングを設定できるのが特徴です

#member設定したルーテイング
/stores/:id/drink
#collection設定したルーテイング
/stores/ice

memberとcollectionの違いは間に
「:id」
があるかないかです。
この機能の実装はいろいろなところで有効活用することができます。ちなみに、
僕はtwitterアプリのいいね機能やブックマーク機能に既に使いました。

ぜひルーテイングをマスターしていきましょう。

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