ネストのみの場合
resources :boards do
resources :comments, only: %i[create destroy update]
end
board_comments POST /boards/:board_id/comments(.:format) comments#create
comment PATCH /boards/:board_id/comments/:id(.:format) comments#update
PUT /boards/:board_id/comments/:id(.:format) comments#update
DELETE /boards/:board_id/comments/:id(.:format) comments#destroy
shallow
shallowオプションは例えば/boards/:board_id/comments/:id
のように、id
が2つパスに含まれているものを、片方のid
だけで十分な場合に使用する。
▷shallowオプション
resources :boards, shallow: true do
resources :comments, only: %i[create destroy update]
end
board_comments POST /boards/:board_id/comments(.:format) comments#create
comment PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
namespace
namespace
メソッド(名前空間)を用いてルーティングをグループ化すると、指定した名前空間名でルーティンングをグループ化することができる。
URLもcontroller格納フォルダも、指定のパスになる。
▷【Rails】ルーティングのグループ化(scope / scope module / namespace)
▷routeのmoduleとnamespaceとscopeの違い
namespace :boards do
resources :commnets
end
boards_comments POST /boards/comments(.:format) boards/comments#create
boards_comment PATCH /boards/comments/:id(.:format) boards/comments#update
PUT /boards/comments/:id(.:format) boards/comments#update
DELETE /boards/comments/:id(.:format) boards/comments#destroy
scope
scopeのみの場合はURLだけ、指定のパスになる。
▷routeのmoduleとnamespaceとscopeの違い
scope '/admin' do
resources :users
end
Prefix Verb URI Pattern Controller#Action
users GET /admin/users(.:format) users#index
POST /admin/users(.:format) users#create
new_user GET /admin/users/new(.:format) users#new
edit_user GET /admin/users/:id/edit(.:format) users#edit
user GET /admin/users/:id(.:format) users#show
PATCH /admin/users/:id(.:format) users#update
PUT /admin/users/:id(.:format) users#update
DELETE /admin/users/:id(.:format) users#destroy
scope module
controller
の格納フォルダだけ指定パスになる。
▷routeのmoduleとnamespaceとscopeの違い
scope module: :admin do
resources :users
end
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) admin/users#index
POST /users(.:format) admin/users#create
new_user GET /users/new(.:format) admin/users#new
edit_user GET /users/:id/edit(.:format) admin/users#edit
user GET /users/:id(.:format) admin/users#show
PATCH /users/:id(.:format) admin/users#update
PUT /users/:id(.:format) admin/users#update
DELETE /users/:id(.:format) admin/users#destroy
member
resources
では自動で生成されないルーティングを設定するときに、**:idを使用する
**アクションを追加するとき。(create、update以外のアクションなど)
▷【Rails】memberとcollectionの違い
resources :boards do
member do
get :commnets,on: :create,:update,:destroy
end
end
comments POST /boards/:id/comments(.:format) comments#create
comment PATCH /boards/:id/comments/:id(.:format) comments#update
PUT /boards/:id/comments/:id(.:format) comments#update
DELETE /boards/:id/comments/:id(.:format) comments#destroy
collection
resources
では自動で生成されないルーティングを設定するときに、**:idを使用しない
**アクションを追加するとき。(create、update以外のアクションなど)
▷【Rails】memberとcollectionの違い
resources :boards do
collection do
get :comments,on: :create,:update,:destroy
end
end
comments POST /boards/comments(.:format) comments#create
comment PATCH /boards/comments/:id(.:format) comments#update
PUT /boards/comments/:id(.:format) comments#update
DELETE /boards/comments/:id(.:format) comments#destroy