Ruby on Rails Route Helpers vs Paths
2021-11-08 update: When I first wrote this post I thought this was a better way of writing links, specially after seeing it done this way in a professional setting. However, it may not be best practice. It’s best to stick with Rails helpers since they’re dynamic. If routes change, the helpers may not need to be changed at all. I found this article that covers best practices better than I can: Rails URL Helpers Readme
While learning how to write tests for requests, I came across a different way of writing links in Rails. Up until this discovery I’ve used URL helpers as such:
|
|
By checking the application’s routes, we can see URL helpers (1st column) and URI Patterns (3rd column).
|
|
URL helpers are mapped URI patterns, but we can also use these endpoints directly. In this case, the new_account
URL helper is the same as /accounts/new
. So the previous link can be changed to:
|
|
If a link requires an ID, we can add it through string interpolation. So the following link…
|
|
…can be rewritten like this:
|
|
This also applies to nested routes. Lets say we have the following in routes.rb
:
|
|
This block generates the following routes for the statement
model:
|
|
If we want to create a link to edit a statement using a URL helper, we would write the following. (Notice we need to pass in two different IDs):
|
|
We can rewrite the link using a URI pattern instead.
|
|
Both URL helpers and URI patterns get the job done, but I enjoyed using URI patterns for familiarity. If an external application is going to communicate with a Rails API, they’ll use the same endpoints defined in URI patterns. It keeps things a bit more consistent.