← Back to Upcase

Pluralzing model-less controllers, which read better in singular


(Silviu Bojica) #1

In Week 4 of Intro to Rails Workshop, I have noticed the use of pluralized controllers like homes_controller, where probably is more natural to read it as home_controller. Its path/route is hidden in this case by being mapped to root_path. But, if we wanted to expose it, the url will read something like http://localhost:3000/homes .

Whilst it makes sense to pluralize the controller’s names when they are tight to a model, what is the best practice when they are model-less, and it is just more natural to read them in singular?

  1. As the plural is just a convention, use the singular in naming our controllers in such cases. (e.g. home_controller, dashboard_controller, search_controllers, etc.)
  2. Use the plural in naming the controllers (e.g. homes_controller, dashboards_controller, searches_controller, etc.), but mapping them with resource in singular, in the config/routes.rb file.

Thanks,

Silviu


(Joel Quenneville) #2

There are two issues here: naming the controller and naming the route(s).

If you are dealing with a singular resource (such as a home page or a dashboard model) you should define a singular route with resource rather than resources. This does not generate an a plural route to the index and edit/update/delete no longer need an id.

Although you would define a singular route, convention is to use a plural controller. According to the rails routing guide:

Because you might want to use the same controller for a singular route (/account) and a plural route (/accounts/45), singular resources map to plural controllers. So that, for example, resource :photo and resources :photos creates both singular and plural routes that map to the same controller (PhotosController).


(Scott Knight) #3

I’m assuming that the homes controller would be plural in the fact that you may also want to add other static pages as part of that controller in the future, e.g. homes#about, homes#contact etc.