115

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?

allyourcode
  • 21,871
  • 18
  • 78
  • 106

9 Answers9

162

Definitely plural.

With restful routing and a singular controller

Controller:

dog_controller.rb  

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

Routes:

map.resources :dogs  
dogs_path # => ok  
dog_path # => ok  

rails generate controller --help has plural examples:

Example:
`rails generate controller CreditCards open debit credit close`

CreditCards controller with URLs like /credit_cards/debit.
    Controller: app/controllers/credit_cards_controller.rb
    Test:       test/controllers/credit_cards_controller_test.rb
    Views:      app/views/credit_cards/debit.html.erb [...]
    Helper:     app/helpers/credit_cards_helper.rb
vaibhavatul47
  • 2,766
  • 4
  • 29
  • 42
jpgeek
  • 4,991
  • 2
  • 28
  • 26
  • 23
    agreed. It's confusing that Rails 3.1 generator help message for controllers uses "CreditCard" (singular) as an example. – bantic Oct 21 '11 at 21:56
  • 4
    Rails help now uses plural: rails generate controller CreditCards open debit credit close – notapatch Jun 27 '13 at 14:48
  • 3
    still has singular CreditCard here: http://guides.rubyonrails.org/command_line.html#rails-generate – rcrogers Mar 05 '14 at 19:27
  • How can we write locales for singular controller http://stackoverflow.com/questions/29650094/add-locales-to-singular-controller-in-rails4 – santosh Apr 15 '15 at 12:34
  • So the naming should be plural and came case. e.g: :rails generate controller Dogs new index create delete destroy edit" ?? – BenKoshy Oct 25 '15 at 05:48
29

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
  • 109,922
  • 25
  • 130
  • 137
  • 10
    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 Mar 15 '09 at 07:16
  • 5
    @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 Mar 15 '09 at 11:50
  • 1
    oh, and it's the RESTful way. – Can Berk Güder Mar 15 '09 at 11:50
  • 4
    @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 Nov 28 '09 at 00:21
  • 3
    @allyourcode According to [this](http://edgeguides.rubyonrails.org/routing.html#resource-routing-the-rails-default) the default route for edit is /users/:id/edit instead of /users/edit. Saying "out of all users, edit the user with id :id" sounds perfectly natural to me. – DavidGamba Apr 10 '13 at 03:15
  • cool, but if you don't want to have the route /users/:id/edit (but instead /user) and/or you don't need user index, just create a singular controller. You actually save authorization overhead too in that way. Rails has many flows regarding RESTful way, so sticking to the convention would only help other developers. – vasilakisfil May 20 '14 at 12:33
21

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
  • 5,089
  • 2
  • 35
  • 43
13

You have a very complete explanation in the Rails guides: http://edgeguides.rubyonrails.org/routing.html#resource-routing-the-rails-default

Nerian
  • 15,901
  • 13
  • 66
  • 96
  • 4
    actually this is the right answer b/c if you read it, it explains that plural is the right answer for a collection of resources. For a singleton resource, singular is the right answer. Examples in the documentation. And actually, this is well answered in this other post: http://stackoverflow.com/questions/2614858/ruby-on-rails-differentiating-plural-vs-singular-resource-in-a-rest-api – Rob Jun 12 '11 at 19:44
  • Answers backed by official references like in this post helps newbies a lot ! Thank you – Wasif Hossain Jul 16 '16 at 11:07
10

It is the Rails convention that one controller handles one model, whether one or more instances of that model can exist during runtime. However, you can have a Rails application where (some of) the controllers (and the associated views) are not associated with any particular model, but rather handle a more complex set of functionality. In this case, the automatic pluralization doesn't make any sense.

The Rails application I'm currently working on fits into this category, and it's simply an irritation to me that Rails expects that the identifiers I define as a singular in one place are then used in their plural forms in other places. For example, I might want to define something like this in config/routes.rb:

  resource :dashboard, :only => [:show]

and then I want a controller DashboardController to display summary information about certain aspects of the application, gathering information from more than one database table. So here, Dashboard does not refer to any model of the application, and it would be just weird to have the controller's name be DashboardsController.

I found a good solution to the irritation of automatic pluralization in this answer. In short, edit file config/initializers/inflections.rb and add the words you don't want to be automatically pluralized to this definition:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable %w( dashboard foo bar baz )
end
Community
  • 1
  • 1
Teemu Leisti
  • 3,750
  • 2
  • 30
  • 39
5

If the controller is a resource then it must be plural...

For example

Controller

articles_controller.rb

Model

article.rb

But you can use singular controller names when you do not have corresponding models like

welcome_controller.rb
4

The naming convention of controllers in Rails favors pluralization of the last word in the controller's name, although it is not strictly required (e.g. ApplicationController).

For example, ClientsController is preferable to ClientController, SiteAdminsController is preferable to SiteAdminController or SitesAdminsController, and so on.

Following this convention will allow you to use the default route generators (e.g. resources, etc) without needing to qualify each :path or :controller, and will keep URL and path helpers' usage consistent throughout your application.

Ref: Controller Naming Convention-Rails Doc

Mukesh Singh Rathaur
  • 12,577
  • 2
  • 23
  • 24
1

I feel better when I use singular for Controller name

1

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.

nitecoder
  • 5,496
  • 1
  • 28
  • 35
  • 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 Mar 15 '09 at 07:18
  • 1
    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. – nitecoder Mar 15 '09 at 10:34