Read more

Rails routing: Using constraints to avoid "Missing template" errors

Deleted user #4117
October 08, 2019Software engineer

You can use constraints in your routes.rb to avoid getting errors when wrong routes are called. Instead, the user will see a 404.

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

If you want multiple routes to use the same constraint you can use the block syntax:

constraints(format: 'html') do
  resources :pages
  resources :images
end

If you want constraints only on certain routes, you can do:

get '/users/account' => 'users#account', constraints: { format: 'html' }

Note: You can also avoid this error through format constraints in your controllers.

Posted to makandra dev (2019-10-08 10:23)