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.

RailsConf 2024

I'm co-chairing RailsConf 2024 in Detroit May 7–9. Come and join us 

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

Instead of…

…generating all the routes:

config/routes.rb

resources :dogs do
  resources :meals
end

Use…

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

config/routes.rb

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

Why?

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.

Brighton Ruby 2024

Still running UK’s friendliest, Ruby event on Friday 28th June. Ice cream + Ruby 


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.