Purple-lit urban road junction

image by Denys Nevozhai

Be specific when defining your routes

Many controllers don’t need the full set of restful routes to be generated. We might have models in our codebase that aren’t user-deletable or are simple enough not need a full #show view.

In those cases you don’t need to generate the routes or actions.

Instead of…

…generating all the routes:


resources :dogs do
  resources :meals


…only the routes you’re actually going to use.


resources :dogs, only: %w[create edit index new show update] do
  resources :meals, only: %w[create index]


There’s no value to maintaining, generating, and loading routes that do not link to actions in your controllers. I may result in errors creeping into your code and unexpected behaviour to live on in your application.

If a user can’t destroy a Thing in a ThingsController, don’t give them a route to try and do so.

Why not?

It might be faster at the point of creation to just leave all those superflouous routes around, but it feels like a poor decision for the maintainability of your app.

Last updated on March 22nd, 2021 by @andycroll

An email newsletter, with one Ruby/Rails technique delivered with a ‘why?’ and a ‘how?’ every two weeks. It’s deliberately brief, focussed & opinionated.