Singular or plural controller and helper names in Rails

Ruby on-RailsNaming Conventions

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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

Solution 7 - Ruby on-Rails

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

Solution 8 - Ruby on-Rails

I feel better when I use singular for Controller name

Solution 9 - Ruby on-Rails

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionallyourcodeView Question on Stackoverflow
Solution 1 - Ruby on-RailsjpgeekView Answer on Stackoverflow
Solution 2 - Ruby on-RailsCan Berk GüderView Answer on Stackoverflow
Solution 3 - Ruby on-RailsrxgxView Answer on Stackoverflow
Solution 4 - Ruby on-RailsNerianView Answer on Stackoverflow
Solution 5 - Ruby on-RailsTeemu LeistiView Answer on Stackoverflow
Solution 6 - Ruby on-Railsuser5650203View Answer on Stackoverflow
Solution 7 - Ruby on-RailsMukesh Singh RathaurView Answer on Stackoverflow
Solution 8 - Ruby on-RailsDillone Hailei WangView Answer on Stackoverflow
Solution 9 - Ruby on-RailsnitecoderView Answer on Stackoverflow