3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

shallow / namespace / scope / scope module / member / collectionの違いをぱっと見する表

Last updated at Posted at 2022-02-03

ネストのみの場合

routes.rb
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オプション

routes.rb
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の違い

routes.rb
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の違い

routes.rb
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の違い

routes.rb
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の違い

routes.rb
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の違い

routes.rb
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
3
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?