Router Configuration
The config
directory contains the configuration files for your Rails application. When you create a new Rails application, the application generator creates a config/routes.rb
file which contains all the routes for your application. This gives a central place to look for all the paths and pages supported by your application.
To configure the router, you add specific rules (called routes) in the routes.rb
file. These routes tell the router how to map incoming URLs to specific controller actions.
All the routes for your application live in the config/routes.rb
file. Here's the default content of this file:
Rails.application.routes.draw do
get "up", to: "rails/health#show", as: :rails_health_check
# Defines the root path route ("/")
# root "posts#index"
end
It's a plain Ruby script. As you can see, it calls the draw method passing a block. The routes are configured inside the block.
If you're not much familiar with Ruby, this configuration file might look weird, unlike a JSON or YAML configuration file.
In its essence, it's a plain Ruby script containing a bunch of method calls. Since Ruby is so expressive, these method calls can be read as instructions, making Ruby a routing-domain specific language (DSL), i.e. a language focused on routing domain, with words like root
, get
, post
, and so on.
I know you're wondering where the methods used in this file are coming from.
When Rails reads this file, it evaluates its contents in the context of a ActionDispatch::Routing::Mapper
class. It contains most of the methods used in the routing file. Don't worry, we'll learn how it all works behind the scenes in the next chapter.
Let's look at the first line of actual code in the routing file.
# config/routes.rb
get "up", to: "rails/health#show", as: :rails_health_check
Here, we're calling the get
method. This route instructs Rails to pass any request to the /up
endpoint URL to the show
action method defined in the Rails::HealthController
class defined in the rails/health_controller.rb
file.
It's important to note that for each incoming HTTP request, the router goes through the routes defined in the routes.rb
file in the order in which they're defined, from top to bottom. If the first route fails to match, it tries the next, and so on, until it finds a matching route which matches the URL pattern (pages). As soon as it finds a matching route, the search ends and the router exits, without trying the remaining routes.
Each route has a corresponding controller#action
method associated with it which gets called to generate the response HTML for that request.
How to List and Filter Routes
You can list all the routes in your Rails application by running the bin/rails routes
command in the terminal.
$ bin/rails routes
Prefix Verb URI Pattern Controller#Action
sidekiq /sidekiq(.:format) Sidekiq::Web
sidekiq_admin /admin Sidekiq::Admin
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
The output shows a table with four columns:
Prefix
: Name of the routeVerb
: HTTP Method such as GET, POST, etc.URI Pattern
: URL pattern to matchController#Action
: Names of the controller class and action method
Filtering Routes
To filter the routes, you can use the grep option, by passing the -g
flag. The following commands filters all the routes containing the term songs
.
$ bin/rails routes -g songs
Prefix Verb URI Pattern Controller#Action
songs GET /songs(/:genre)(.:format) songs#index {:genre=>"rock"}
View in a Browser
Alternatively, you could also find all the routes by visiting the /rails/info/routes
path on a running Rails application.
So that was a brief introduction to the routing process and the Rails router. Now let's inspect the internals of the router to understand what's going on behind the scenes. Trust me, this will be really useful as you learn more advanced features of the router later in the article.
If you have any questions or feedback, didn't understand something, or found a mistake, please leave a comment below or send me an email. You can also subscribe to my blog to receive future posts via email.