Single resources require a plural controller name

One of the big WTF for me is the one related to the requirement of having to use plural controller names when defining a singular resources.

I know that the guide says:

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 ).

But at least for me in more than 10 years of using rails this never happened. When I defined a singular resource it was always to set it as a singular resource, without the need of accessing the plural form like in the example (by defining the plural resources as well).

What are others’ experiences and thoughts? Should a singular resource map to a singular controller name by default, and if someone has that scenario use the resource :photo, controller: 'photos' to point to the plural controller name?

2 Likes

I agree completely.

A convention I’ve been using is to have a single resource for something that does not need an id in the url (such as /user/settings usually points to the current user controller) and plural resources for something that does (/users/15 points to the user’s public profile page). As of now, I always have to do resource :user, controller: :user which I think is redundant.

I just went through this last week. I was adding a :search route to a new Rails 6 app, and the routes didn’t appear at all (in rake routes) until I switched from resource :search to resources :search, only: :index. My controller is named SearchController. I can’t think of a RESTful reason why I would have more than just that one route.

Walter

1 Like

The way I learned to live with this was by telling myself that even though the route is for a single resource, the controller still handles many requests for different instances of that resource so the pluralization makes sense in that context.

1 Like

I try to adhere to only using resources with plural controllers, never using resource, and instead opting for more targeted get profile, to: ....

If a controller implements show, edit, new, create, destroy, then it is inherently plural.

1 Like