Before I start, I would just like to say that I do really like the rails routing interface and am super appreciative of all the hard work that has gone into its current state.
Over the years, I’ve discovered some patterns that I find useful and am curious what others think.
resources :foos, :only => [:index, :create] do
resources :bars, :only => [:index]
end
This code expects you to define FoosController
and BarsController
. Anymore, I always do the following:
resources :foos, :only => [:index, :create] do
resources :bars, :only => [:index], :module => :foos
end
The difference is that it expects FoosController
and Foos::BarsController
. In other words, you now have a bars controller that’s specifically for bars in the context of a foo.
This frees you up for the eventual stand-alone bars controller or even a Quxes::BarsController
a la:
resources :foos, :only => [:index, :create] do
resources :bars, :only => [:index], :module => :foos
end
resources :quxes, :only => [:index] do
resources :bars, :only => [:index], :module => :quxes
end
resources :bars, :only => [:index]
Given this organization pattern extends to the controller’s corresponding views, I’ve found it to provide lots of flexibility. IMO, it’s easy for folks to fall into the trap of “I have a Foo
model, I must cram all perspectives of Foo
into a single FooController
”.
Does anyone else do this? Could it be the default for nested routes? Or is it better as-is allowing you to opt in?