Getting Rails Nested Resources to Route to correct nested Controllers
Solution 1
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
Solution 2
resources :foos do
resources :bars, :controller -> 'foos/bars'
resources :bangs, :controller => 'foos/bangs'
end
Isn't this what you require?
Comments
-
CITguy about 2 years
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
. Theresources :bars
should route to thefoos/bars
controller rather than thebars
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. -
CITguy over 11 yearsYes and no. I'd like to NOT have to specify the controller for each resource. In a rails 2.3 app, nesting the resources was enough to map the correct path and controllers.
-
CITguy about 11 yearsYeah, this is the way to do it. I forgot I had this question still on here.
-
Phill Sparks about 11 yearsI figured someone else might still find your question relevant.
-
Sean Magyar over 8 years@PhillSparks, could you take a look at this issue? It's almost the same, but I can't figure it out. stackoverflow.com/questions/35672666/…