Вопрос

In Rails 3.2.11, I have the following route definitions

resources :foos do
    resources :bars
    resources :bangs, :controller => 'foos/bangs'
end

which result in the following routes

     foo_bars GET    /foos/:foo_id/bars(.:format)            bars#index
              POST   /foos/:foo_id/bars(.:format)            bars#create
  new_foo_bar GET    /foos/:foo_id/bars/new(.:format)        bars#new
 edit_foo_bar GET    /foos/:foo_id/bars/:id/edit(.:format)   bars#edit
      foo_bar GET    /foos/:foo_id/bars/:id(.:format)        bars#show
              PUT    /foos/:foo_id/bars/:id(.:format)        bars#update
              DELETE /foos/:foo_id/bars/:id(.:format)        bars#destroy
    foo_bangs GET    /foos/:foo_id/bangs(.:format)           foos/bangs#index
              POST   /foos/:foo_id/bangs(.:format)           foos/bangs#create
 new_foo_bang GET    /foos/:foo_id/bangs/new(.:format)       foos/bangs#new
edit_foo_bang GET    /foos/:foo_id/bangs/:id/edit(.:format)  foos/bangs#edit
     foo_bang GET    /foos/:foo_id/bangs/:id(.:format)       foos/bangs#show
              PUT    /foos/:foo_id/bangs/:id(.:format)       foos/bangs#update
              DELETE /foos/:foo_id/bangs/:id(.:format)       foos/bangs#destroy
         foos GET    /foos(.:format)                         foos#index
              POST   /foos(.:format)                         foos#create
      new_foo GET    /foos/new(.:format)                     foos#new
     edit_foo GET    /foos/:id/edit(.:format)                foos#edit
          foo GET    /foos/:id(.:format)                     foos#show
              PUT    /foos/:id(.:format)                     foos#update
              DELETE /foos/:id(.:format)                     foos#destroy

The paths are all correct, but the controller routing is only correct for resources :bangs. The resources :bars should route to the foos/bars controller rather than the bars controller.

It used to be that I could set up namespaced controllers within foos/ to handle the nested resource.

#app/controllers/foos/bars_controller.rb

class Foos::BarsController < ApplicationController
    #/foos/:foo_id/bar/:id available here
end

However this doesn't seem to be the case any more. When did this behavior change and how do I get the same functionality in Rails 3.2.11?

EDIT: I realize the resources :bangs results in the correct controller mapping, but I'd like to be able to have that done implicitly. If I have several nested resources within :foos, I don't want to have to define the controller for every resource.

Это было полезно?

Решение

You can add a scope to specify the module. Rails assumes that the controllers for your nested resources are not themselves nested.

resources :foos do
    scope module: :foos do
        resources :bars
        resources :bangs
    end
end

Другие советы

resources :foos do
  resources :bars, :controller -> 'foos/bars'
  resources :bangs, :controller => 'foos/bangs'
end

Isn't this what you require?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top