views:

2812

answers:

5

Is there any disadvantage to using singular names for controllers and helpers? Nothing seems to rely on this. It even seems helpers don't have to make the same choice about singular vs. plural as their corresponding controllers, at least according to my limited experimentation. Is that true?

+3  A: 

Using plural names for controllers is just a convention.

Plural names usually sound more natural (especially for controllers that are tied directly to a specific model: User -> Users, etc.), but you can use whatever you want.

As for helpers, all helpers are available for all controllers by default, so technically, how you name your helpers doesn't matter at all. It's just another convention to keep a controller's helper functions in a helper with the same name as the controller.

Can Berk Güder
Wouldn't it be more natural for the controller corresponding to User be the UserController?? Also, if you rely on the default routes, you get url's that look like /users/edit, which looks like you're editing all users. To me, that's not very natural at all.
allyourcode
@allyourcode: well, I guess it's all subjective. to me, having /users list all the users is more natural than /user.
Can Berk Güder
oh, and it's the RESTful way.
Can Berk Güder
@Can "the RESTful way" sounds like a cultish chant. That doesn't really surprise me though, as Rails is pretty religious overall.I like how Rails is all obsessed about REST, yet default routes are not restful. Even configuring RESTful routes is unnatural. Including :conditions => {:method => :post} in the second argument to connect makes no sense, as the hash is supposed to specify how to handle any request that matches the current rule, not _whether_ any given request matches the current rule.
allyourcode
+1  A: 

Using plurals just sounds better, and then if you have a controller that handles a singular resourse, ie user, then you can still name the url /user.

With helpers there is often no need to have a helper for every controller, and often there will be helper methods you can use ascorss multiple controllers and rather litter them all through your application helper you could put them in custom helpers instead like eg layout_helper or any other well named file.

railsninja
Same comments as for Can Berk Guder. In addition, I had some trouble following your last sentence/paragraph because there was so little punctuation!
allyourcode
Sorry about that, all I meant was that it may be a better idea to create custom helpers rather than use the defaults as the name of the default ones don't always capture fully where they are going to be used. If you have a number of helper methods that will be used for layout, call it layout_helper.
railsninja
A: 

I feel better when I use singular for Controller name

Dillone Hailei Wang
+1  A: 

A Model is singular because it references a single object like User. A controller is plural because it is the controls (methods) for the collection of Users. How one names the routes is all up to that individual developer. I've never had a user complain that a URL for a web request is singular or plural. The end result to maintain a common convention for current and future contributors while serving quality page displays or the API requests for the end users.

rxgx
A: 

Definitely plural.

With restful routing and a singular controller:

controller: dog_controller.rb
in routes:
map.resources :dogs # => blows up
map.resources :dog # is ok, but...
dogs_path # => blows up
dog_path # => ok

Using a plural controller:

controller: dogs_controller.rb
in routes:
map.resources :dogs
dogs_path # => ok
dog_path # => ok

script/generate controller --help has singular examples though. Ugh.

jpgeek