[LRUG] Rails Question : Matching Share Nested Resources to Controllers

Tom Stuart tom at experthuman.com
Mon Nov 5 05:49:48 PST 2007


On 5 Nov 2007, at 13:16, Anthony Green wrote:
> Should each one have their own controller ?
> books_controller.rb
> authors_books_controller.rb
> publishers_books_controller.rb
>
> Or a single controller with some form of contextual checking ?
>
> The former seems preferable to me as less opaque

For what it's worth I'd favour the latter approach, because  
fundamentally you're doing the same thing in all cases: manipulating a  
collection of books, which may (or may not) be scoped by a particular  
parent object. The actions of the controller don't need to be  
obfuscated to account for this -- just have a method that returns the  
parent object, and have the controller actions pay the appropriate  
attention to that object if it's not nil. To me this is easier to  
understand than having three controllers which all do the same  
conceptual job in slightly different ways; perhaps it comes down to a  
personal sense of what constitutes "opacity".

If the views are heavily contextual then clearly they'll need to be  
separated out anyway, but that doesn't mean that the controllers need  
to be. Just put the contextual bits of the view in partials and render  
them as appropriate. It is tempting to relegate the view-selection  
problem to Rails by having separate controllers and letting it  
automatically render the default view for each, but I don't think  
that's a net win given the cost and how simple it is to do it yourself.

Cheers,
-Tom



More information about the Chat mailing list