Rake up routes in Rails
RUBY ON RAILS
Jul 26 2017 · 1 minutes read
As a Ruby on Rails application grows it can be difficult to keep track which routes map to which controller and action. Rails attempts to combat this by relying on convention over configuration. This helps to reduce the cognitive load on an engineer. But a convention is just a convention.
What happens when the convention isn't followed? How do you quickly find which route maps to a controller action? Luckily Rails has the answer.
In Rails there is a Rake task that lists all of the routes. For each route it specifies the route helper, the HTTP verb and the controller action:
$ bundle exec rake routes
Prefix Verb URI Pattern Controller#Action
user_bookmarks POST /users/:user_id/bookmarks(.:format) bookmarks#create
new_user_bookmarks GET /users/:user_id/bookmarks/new(.:format) bookmarks#new
edit_user_bookmarks GET /users/:user_id/bookmarks/edit(.:format) bookmarks#edit
GET /users/:user_id/bookmarks(.:format) bookmarks#show
PUT /users/:user_id/bookmarks(.:format) bookmarks#update
DELETE /users/:user_id/bookmarks(.:format) bookmarks#destroy
Having all of this information in one place makes it trivial to trace requests in your application. The next time you need to find a route rake it up in Rails.