読者です 読者をやめる 読者になる 読者になる

woshidan's loose leaf

ぼんやり勉強しています。

config/routes.rbのshallow: trueとは

Rails Ruby route

仕事で見てよく分からなかったので調べてみた。

http://qiita.com/kuboon/items/96bbd227f9497ed81f38

resources :groups do
  resources :users
end

resources :users
     group_users GET    /groups/:group_id/users(.:format)          users#index
                 POST   /groups/:group_id/users(.:format)          users#create
  new_group_user GET    /groups/:group_id/users/new(.:format)      users#new
 edit_group_user GET    /groups/:group_id/users/:id/edit(.:format) users#edit
      group_user GET    /groups/:group_id/users/:id(.:format)      users#show
                 PATCH  /groups/:group_id/users/:id(.:format)      users#update
                 PUT    /groups/:group_id/users/:id(.:format)      users#update
                 DELETE /groups/:group_id/users/:id(.:format)      users#destroy
          groups GET    /groups(.:format)                          groups#index
                 POST   /groups(.:format)                          groups#create
       new_group GET    /groups/new(.:format)                      groups#new
      edit_group GET    /groups/:id/edit(.:format)                 groups#edit
           group GET    /groups/:id(.:format)                      groups#show
                 PATCH  /groups/:id(.:format)                      groups#update
                 PUT    /groups/:id(.:format)                      groups#update
                 DELETE /groups/:id(.:format)                      groups#destroy
           users GET    /users(.:format)                           users#index
                 POST   /users(.:format)                           users#create
        new_user GET    /users/new(.:format)                       users#new
       edit_user GET    /users/:id/edit(.:format)                  users#edit
            user GET    /users/:id(.:format)                       users#show
                 PATCH  /users/:id(.:format)                       users#update
                 PUT    /users/:id(.:format)                       users#update
                 DELETE /users/:id(.:format)                       users#destroy
resources :groups, shallow: true do
  resources :users
end
     group_users GET    /groups/:group_id/users(.:format)     users#index
                 POST   /groups/:group_id/users(.:format)     users#create
  new_group_user GET    /groups/:group_id/users/new(.:format) users#new
       edit_user GET    /users/:id/edit(.:format)             users#edit
            user GET    /users/:id(.:format)                  users#show
                 PATCH  /users/:id(.:format)                  users#update
                 PUT    /users/:id(.:format)                  users#update
                 DELETE /users/:id(.:format)                  users#destroy
          groups GET    /groups(.:format)                     groups#index
                 POST   /groups(.:format)                     groups#create
       new_group GET    /groups/new(.:format)                 groups#new
      edit_group GET    /groups/:id/edit(.:format)            groups#edit
           group GET    /groups/:id(.:format)                 groups#show
                 PATCH  /groups/:id(.:format)                 groups#update
                 PUT    /groups/:id(.:format)                 groups#update
                 DELETE /groups/:id(.:format)                 groups#destroy

つまり、

resources :groups, shallow: true do
  resources :users
end
resources :groups do
  resources :users, only: [:new, :create, :index]
end

resources :users, only: [:edit, :show, :update, :destroy]

ということかー!?

個人的に、階層構造を示したくてネスト構造のルート書いたんだから、後者の書き方の方が好きではある。

(というか、善し悪し考えず新しければgemであれば、みたいな書き方はさっきの記事でも書いたけど、嫌いなんである。お題目は考え方の補助ではあるんだけど、それのすべてではないよね、それだったらRSSリーダ購読機能のついたコードジェネレータにやらせれば私らそのうち要らんくなるでしょ、みたいな悲観症というか)。