Read more

Rails index route for resources named after uncountable substantives

Dominik Schöler
January 28, 2015Software engineer at makandra GmbH

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.


Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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
Posted by Dominik Schöler to makandra dev (2015-01-28 15:25)