モデルの親子関係でルーティングをネストできる
2.7 ネストしたリソース
他のリソースの配下に論理的な子リソースを配置することはよくあります。たとえば、Railsアプリケーションに以下のモデルがあるとします。
class Magazine < ApplicationRecord
has_many :ads
end
class Ad < ApplicationRecord
belongs_to :magazine
end
ルーティングをネストする(入れ子にする)宣言を使うことで、この親子関係をルーティングで表せるようになります。
resources :magazines do
resources :ads
end
リソースのネスティングの深さは、経験則として1階層にとどめておくべきです。
:only
オプションで生成するルーティングを指定
ネストを浅くする
(上記で推奨したような)深いネストを回避する方法の1つとして、コレクションアクションの生成場所を親のスコープに移動するという方法があります。この方法を使うと、階層化されたという感覚を得ながら、メンバーアクションをネストしないようにできます。言い換えると、最小限の情報でリソースを一意に指定できるルーティングを作成するということです。
"メンバー"アクションとは、個別のリソースに適用され、アクションの対象となる特定のリソースを識別するためにIDを指定する必要のあるアクション(showやeditなど)を指します。"コレクション"アクションとは、リソースのセット全体を操作するアクション(indexなど)を指します。
resources :articles do
resources :comments, only: [:index, :new, :create]
end
resources :comments, only: [:show, :edit, :update, :destroy]
上のルーティングでは、
:onlyオプションを用いることで、指定したルーティングだけを生成するようRailsに指示
しています。 この方法は、ルーティングの記述を複雑にせずに済み、かつ深いネストを作らずに済むという絶妙なバランスを保っています。:shallowオプションを使うことで、上と同じ内容をさらに簡単に記述できます。
resources :articles do
resources :comments, shallow: true
end
これによって生成されるルーティングは、最初の例と完全に同じです。
親リソースで:shallowオプションを指定すると、すべてのネストしたリソースが浅くなります
。
resources :articles, shallow: true do
resources :comments
resources :quotes
end
試してみた
Rails.application.routes.draw do
resources :articles, shallow: true do
resources :comments
resources :quotes
end
end
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
article_quotes GET /articles/:article_id/quotes(.:format) quotes#index
POST /articles/:article_id/quotes(.:format) quotes#create
new_article_quote GET /articles/:article_id/quotes/new(.:format) quotes#new
edit_quote GET /quotes/:id/edit(.:format) quotes#edit
quote GET /quotes/:id(.:format) quotes#show
PATCH /quotes/:id(.:format) quotes#update
PUT /quotes/:id(.:format) quotes#update
DELETE /quotes/:id(.:format) quotes#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
resources :articles, shallow: true do
resources :comments, only: [:index, :new, :create]
resources :quotes, only: [:index, :new, :create]
end
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
article_quotes GET /articles/:article_id/quotes(.:format) quotes#index
POST /articles/:article_id/quotes(.:format) quotes#create
new_article_quote GET /articles/:article_id/quotes/new(.:format) quotes#new
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
:shallowあり
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
article_quotes GET /articles/:article_id/quotes(.:format) quotes#index
POST /articles/:article_id/quotes(.:format) quotes#create
new_article_quote GET /articles/:article_id/quotes/new(.:format) quotes#new
edit_quote GET /quotes/:id/edit(.:format) quotes#edit
quote GET /quotes/:id(.:format) quotes#show
PATCH /quotes/:id(.:format) quotes#update
PUT /quotes/:id(.:format) quotes#update
DELETE /quotes/:id(.:format) quotes#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
:shallowなし
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PATCH /articles/:article_id/comments/:id(.:format) comments#update
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
article_quotes GET /articles/:article_id/quotes(.:format) quotes#index
POST /articles/:article_id/quotes(.:format) quotes#create
new_article_quote GET /articles/:article_id/quotes/new(.:format) quotes#new
edit_article_quote GET /articles/:article_id/quotes/:id/edit(.:format) quotes#edit
article_quote GET /articles/:article_id/quotes/:id(.:format) quotes#show
PATCH /articles/:article_id/quotes/:id(.:format) quotes#update
PUT /articles/:article_id/quotes/:id(.:format) quotes#update
DELETE /articles/:article_id/quotes/:id(.:format) quotes#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
- すべてのネストしたリソースが浅くなっている