Rails index route for resources named after uncountable substantives

Updated . Posted . Visible to the public.

Using uncountable resources is not recommended as it breaks Rails' magic, e.g. when using form_for. You'll always be better off using simple pluralizable resources.


Rails automatically creates path names for routes defined via the resource method. When you put resource 'user' into config/routes.rb, you can call users_path and get the path to the index action in the UsersController: /users.

However, if you have an uncountable resource like Sheep, you cannot access the index action via sheep_path, because it will refer to the show action: /sheep/:id.

Solution

As the linked article states, Rails creates a special <resource>_index_path for this scenario. Thus, you can simply call:

sheep_index_path
Profile picture of Dominik Schöler
Dominik Schöler
Last edit
Henning Koch
Keywords
singular, plural
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2015-01-28 14:25)