开发者

In MVC frameworks (such as Ruby on Rails), does usually Model spell as singular and controller and view spell as plural?

I usually see Ruby on Rails books using

script/generate model Story name:string link:string

which is a singular Story, while when it is controller

script/generate controller Stories index

then the Story now is Stories, which is plural.

Is this a standard on Ruby on Rails? Is it true in other MVC frameworks too, like CakePHP, Symfony, Django, or TurboGears?

I see that in the book Rails Space, the controller is also ca开发者_如何学运维lled User, which is the same as the model name, and it is the only exception I see.

Update: also, when scaffold is done on Ruby on Rails, then automatically, the model is singular and the controller and view are both plural.


Generally, Rails has a very specific format for naming models, controllers, and views that is not necessarily the only way to accomplish the task. The Rails tradition is:

  • Models are in singular form (ie. Story)
  • Controllers are in plural form (ie. Stories)
  • Views are sorted by folder according to the name of the controller (ie. stories/show.html.erb)

Django appears to use a similar convention with:

  • Models are singular
  • "views" (which are basically controllers) are not necessarily tied to a particular model
  • "templates" do not folow a particular convention, although several exist.

Since CakePHP (especially) seems to follow behind Rails's every move, I would expect that it follows the same convention.

As for the exception, I would imagine that it has to do with URL routing. Before Rails 2.3 (I think), Rails routes were automatically set based on the controller name (now the router has far greater flexibility). While they could be changed, most people chose not to mess about in the router. I would much prefer to visit yourwebsite.com/user/ to visit my home page on the website when logged in than yourwebsite.com/users/22.

Remember: conventions exist for a reason, but there are times when conventions are broken reasonably. Keep "convention of configuration" only when it makes sense.


It really is your choice, however in Ruby on Rails it is a good convention to choose singular names for models, so that by convention Rails creates nice names for the tables etc. The controllers in Rails usually are in plural, because it produces nice routes (URLs), however it does not matter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜