config/routes.rb: the entry point map

Free preview 2 min to read

A route that already made an appearance

In Rails console (IRB), app.get "/articles/1" only worked because a matching entry already existed somewhere. That somewhere is config/routes.rb, the file that decides which URLs an app answers and which controller action handles each.

# config/routes.rb
Rails.application.routes.draw do
  # every route this app answers to is listed inside this block
end

Rails.application.routes.draw do ... end is the file's whole shape: a block where each line declares one more thing the app responds to. Nothing outside the block is a route.

What's inside this project's file

Rails.application.routes.draw do
  get "up" => "rails/health#show", as: :rails_health_check
  # ... many more routes ...
  root "public/pages#home"
end

This is a trimmed view of this project's config/routes.rb, the file mentioned in Rails CLI: rails new. Each line connects one URL pattern and HTTP verb to one controller action. Lessons 2–5 show how those connections are written.

Why a route has to exist first

Before any controller, model or view runs, the request's URL and verb must match something in this file. With no match, the request fails at routing and never reaches the app's own code.

Key takeaways

  • config/routes.rb decides which URLs the app answers and which controller action handles each.
  • Every route lives inside Rails.application.routes.draw do ... end; nothing outside that block is a route.
  • This project's routes.rb has the same shape as any Rails app's.
  • A request must match a route before any controller, model or view code runs.
Lesson 1 of 7 · free

Keep going with lesson 2

Writing one route at a time

Writes single routes from a verb, a URL pattern and an action, and introduces dynamic segments like :username.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...