routeのresourcesのnestした部分をnamespaceにする方法

nestしたresourcesだと、namespaceでcontrollerとviewのファイル置き場が別々になってしまって嫌です。

下のやつです。

resources :foo do
  resouces : bar
end

結果

        foo_bar_index GET    /foo/:foo_id/bar(.:format)                  bar#index
                      POST   /foo/:foo_id/bar(.:format)                  bar#create
          new_foo_bar GET    /foo/:foo_id/bar/new(.:format)              bar#new
         edit_foo_bar GET    /foo/:foo_id/bar/:id/edit(.:format)         bar#edit
              foo_bar GET    /foo/:foo_id/bar/:id(.:format)              bar#show
                      PATCH  /foo/:foo_id/bar/:id(.:format)              bar#update
                      PUT    /foo/:foo_id/bar/:id(.:format)              bar#update
                      DELETE /foo/:foo_id/bar/:id(.:format)              bar#destroy
            foo_index GET    /foo(.:format)                              foo#index
                      POST   /foo(.:format)                              foo#create
              new_foo GET    /foo/new(.:format)                          foo#new
             edit_foo GET    /foo/:id/edit(.:format)                     foo#edit
                  foo GET    /foo/:id(.:format)                          foo#show
                      PATCH  /foo/:id(.:format)                          foo#update
                      PUT    /foo/:id(.:format)                          foo#update
                      DELETE /foo/:id(.:format)                          foo#destroy

ここでbar controllerになるのが、いやでたまりません。

プロジェクトが大きくなるにつれて、ファイルの定義場所に苦労することになると思います。

scope moduleを使います。

scopeについて

そもそもrouteのscopeとはなんでしょうか?

・scopeのみの場合、URLのみ指定したパスになります。

            bar_index GET    /foo/bar(.:format)                          bar#index
                      POST   /foo/bar(.:format)                          bar#create
              new_bar GET    /foo/bar/new(.:format)                      bar#new
             edit_bar GET    /foo/bar/:id/edit(.:format)                 bar#edit
                  bar GET    /foo/bar/:id(.:format)                      bar#show
                      PATCH  /foo/bar/:id(.:format)                      bar#update
                      PUT    /foo/bar/:id(.:format)                      bar#update
                      DELETE /foo/bar/:id(.:format)                      bar#destroy

これにmoduleというのがあるので、つけてみます。

・scope: :moduleは、controllerのアクションのみをnamespaceにします。

            bar_index GET    /bar(.:format)                              foo/bar#index
                      POST   /bar(.:format)                              foo/bar#create
              new_bar GET    /bar/new(.:format)                          foo/bar#new
             edit_bar GET    /bar/:id/edit(.:format)                     foo/bar#edit
                  bar GET    /bar/:id(.:format)                          foo/bar#show
                      PATCH  /bar/:id(.:format)                          foo/bar#update
                      PUT    /bar/:id(.:format)                          foo/bar#update
                      DELETE /bar/:id(.:format)                          foo/bar#destroy

これを利用して解決します。

解決策

  resources :foo do
    scope module: :foo do
      resources :bar
    end
  end

結果

        foo_bar_index GET    /foo/:foo_id/bar(.:format)                  foo/bar#index
                      POST   /foo/:foo_id/bar(.:format)                  foo/bar#create
          new_foo_bar GET    /foo/:foo_id/bar/new(.:format)              foo/bar#new
         edit_foo_bar GET    /foo/:foo_id/bar/:id/edit(.:format)         foo/bar#edit
              foo_bar GET    /foo/:foo_id/bar/:id(.:format)              foo/bar#show
                      PATCH  /foo/:foo_id/bar/:id(.:format)              foo/bar#update
                      PUT    /foo/:foo_id/bar/:id(.:format)              foo/bar#update
                      DELETE /foo/:foo_id/bar/:id(.:format)              foo/bar#destroy
            foo_index GET    /foo(.:format)                              foo#index
                      POST   /foo(.:format)                              foo#create
              new_foo GET    /foo/new(.:format)                          foo#new
             edit_foo GET    /foo/:id/edit(.:format)                     foo#edit
                  foo GET    /foo/:id(.:format)                          foo#show
                      PATCH  /foo/:id(.:format)                          foo#update
                      PUT    /foo/:id(.:format)                          foo#update
                      DELETE /foo/:id(.:format)                          foo#destroy

scope moduleでfooのファイル置き場にするようにしています。 これをresourcesすることで求めていた結果になります。

以上です。

参考

qiita.com