Could somebody please answer Carl Youngblood''s question (http://thread.gmane.org/gmane.comp.lang.ruby.rails/2105) from January 26th definitively? To quote his message: "For the time being, which usage is more common for naming controllers and models? Do you like to put the object being operated on in singular or plural?" In looking through other people''s Rails applications I see that models are consistently in the singular (which makes good sense), but I''ve seen controllers and their corresponding views in both the singular and plural. Which is to say I''ve seen both: http://www.hotstuffonrails.egg/post/view/42 ... and ... http://www.hotstuffonrails.egg/posts/view/42 Is one correct? Is the pluralization that `script/generate scaffold Post` decides on correct? An authoritative answer, please! Advance thanks, al3x
Well I''m not authoritative, but I''ll give it a shot and perhaps someone will back me up. It seems that the closest aproximation to a consensus is to use singular models (in fact AR seems to strongly encourage this) and pluralized controllers. The reasons for the varying codebases out there are, I think, mostly historic. The generators/new_scaffold scripts have done it both ways within the past month or so (rails is a bit of a moving target :). I''m sure a few people prefer it either way and even more have gotten into the habit of one or another based on the scripts being that way when they started their project. In short, singular models are all but required to make AR happy and controllers are up to you, but I (and most everyone I''ve run into, and the generators) prefer to use plurals for controllers. Brian On Mon, 31 Jan 2005 22:48:26 -0500, Alexander Payne <al3x-d9d7LvA1F+o@public.gmane.org> wrote:> Could somebody please answer Carl Youngblood''s question > (http://thread.gmane.org/gmane.comp.lang.ruby.rails/2105) from January > 26th definitively? To quote his message: > > "For the time being, which usage is more common for naming controllers > and models? Do you like to put the object being operated on in singular > or plural?" > > In looking through other people''s Rails applications I see that models > are consistently in the singular (which makes good sense), but I''ve > seen controllers and their corresponding views in both the singular and > plural. Which is to say I''ve seen both: > > http://www.hotstuffonrails.egg/post/view/42 > > ... and ... > > http://www.hotstuffonrails.egg/posts/view/42 > > Is one correct? Is the pluralization that `script/generate scaffold > Post` decides on correct? An authoritative answer, please! > > Advance thanks, > al3x > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
On Mon, 31 Jan 2005 22:48:26 -0500, Alexander Payne <al3x-d9d7LvA1F+o@public.gmane.org> wrote:> "For the time being, which usage is more common for naming controllers > and models? Do you like to put the object being operated on in singular > or plural?"> In looking through other people''s Rails applications I see that models > are consistently in the singular (which makes good sense), but I''ve > seen controllers and their corresponding views in both the singular and > plural. Which is to say I''ve seen both:> Is one correct? Is the pluralization that `script/generate scaffold > Post` decides on correct? An authoritative answer, please!I think that the mistake many people are making is tying the idea of the controller too closely to the idea of the model. A controller should simply be named sensibly according to what it _does_. If your controller is simply a CRUD interface for a single type of model, then it probably makes sense to use a pluralized version of the model name in the controller; but in more complex scenarios this doesn''t necessarily hold. For instance, if I''m creating an ecommerce site, I might have one controller that outputs the "catalog" portion of the site. This would deal with several different model objects such as Product, Category, Review, etc. While you _could_ create a seperate controller for each of these, I like to put the whole "catalog" in one CatalogController. Basically, keep similar ideas in one place based on what types of operations the _user_ is performing. That''s just my 2 cents, though. -- Regards, John Wilger ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland
On Tue, 1 Feb 2005 09:17:55 -0500, John Wilger <johnwilger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I think that the mistake many people are making is tying the idea of > the controller too closely to the idea of the model. A controller > should simply be named sensibly according to what it _does_. If your > controller is simply a CRUD interface for a single type of model, then > it probably makes sense to use a pluralized version of the model name > in the controller; but in more complex scenarios this doesn''t > necessarily hold. For instance, if I''m creating an ecommerce site, I > might have one controller that outputs the "catalog" portion of the > site. This would deal with several different model objects such as > Product, Category, Review, etc. While you _could_ create a seperate > controller for each of these, I like to put the whole "catalog" in one > CatalogController. Basically, keep similar ideas in one place based on > what types of operations the _user_ is performing.I''m not really adding much to answer the original question asked, but I wanted to post and say that I whole heartedly agree with John. model = data, controller = manipulation of data, view = display of data. I have controllers that need access to several data sources, because it is the most logical layout. For these, I tend to use names that make sense to me (I''m working on webzine system), so article contributions are handled by the contribute controller, whereas displaying the actual article on the site is handled by an article controller. The reason for this is the contribute controller requires account-related nonsense (only people with the right access level can contribute new articles), but anybody can view them. This design allows me to keep the pieces separate and not clutter up the article display (which is really simple) with account nonsense and access controls. Hope this helps. Ben