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 money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
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)