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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)