...

/

Use Canonical Routes to Conform Rails’ Defaults

Use Canonical Routes to Conform Rails’ Defaults

Learn about routes that we should use in Rails applications.

What are routes used for?

Routes serve two purposes. Their primary purpose is to connect the view to the controller layer. Routes tell us what code will be triggered when an HTTP request is made to a URL. The second (and unfortunate) purpose of routes is as a user interface element. URLs tend to show up directly in social media, search results, and even newspaper articles. This means that a user will see them and that they matter.

It can be hard to design routes that serve both purposes. If our routes are designed first around aesthetic concerns, we’ll quickly have a sea of inconsistent and confusing URLs, and this will create a carrying cost on the team every time a new feature has to be added. We also can’t insist that our app is only available with conventional Rails routes. Imagine someone reading a podcast ad with a database ID in it!

However, the marketing department isn’t the only source of complexity with our routes. The more routes we add and the more features our app supports, the harder it can be to keep the routes organized. If routes become messy, inconsistent, or hard to understand, it adds carrying costs with every new feature we want to ...