Rails Routes Basics
Fast recall
Quick revision
Core answer
config/routes.rb; for example, get "/health", to: "health#show" sends GET /health to HealthController#show. resources :articles generates the seven conventional REST routes for index, show, new, create, edit, update, and destroy, along with named URL helpers. Rails uses the first matching route, so declaration order matters.Whether you know routes live in config/routes.rb, the forms (root, get, resources), and that resources :posts gives the 7 routes + helpers.
Hand-writing the 7 routes when resources :posts generates them, or forgetting root (no homepage).
Routes map URLs to actions in config/routes.rb: root, get 'x', to: 'y#z', resources :posts (7 routes + helpers). Check with rails routes.
Interview-ready guide
Interview answer
Rails routing is the dispatch table between HTTP and controller actions. Each route describes a verb, a path pattern, and a destination such as articles#show. A request must match the verb as well as the URL; GET /articles/7 and DELETE /articles/7 can intentionally reach different actions.
Dynamic segments are captured as parameters. With get 'articles/:id', to: 'articles#show', the URL /articles/7 produces params[:id] == '7'. Named route helpers build URLs from the same declaration, avoiding hard-coded paths throughout the application.
For example, resources :articles creates collection routes for listing and creating articles, member routes for reading, editing, updating, and deleting one article, and form routes for new and edit. It also creates helpers such as articles_path and article_path(article).
The router tests declarations from top to bottom. A broad dynamic route placed too early can capture a path intended for a specific route. Routes should identify resources and HTTP semantics; authentication and domain work usually belong after routing, in controllers or other application layers.
bin/rails routes shows the helper, verb, path, and target and is the first tool for diagnosing a routing mismatch. A clear resource route keeps URL design, controller naming, and request intent aligned. Recognition and URL generation then share one authoritative declaration instead of separate hard-coded strings reliably.
Rails Routes Basics
Direct Answer
A Rails route matches an HTTP verb and URL pattern to a controller action. Routes live in config/routes.rb; for example, get "/health", to: "health#show" sends GET /health to HealthController#show. resources :articles generates the seven conventional REST routes for index, show, new, create, edit, update, and destroy, along with named URL helpers. Rails uses the first matching route, so declaration order matters.
Deep Dive & Architecture
The router recognises requests and generates URLs from the same definitions. Recognition converts a verb and path into controller parameters. Generation uses a named helper and values to build a path. Using helpers means a later path change is reflected without searching for string literals across views and controllers.
RESTful resources distinguish collection operations from member operations. index and create address the article collection; show, update, and destroy address one member identified by :id. new and edit serve HTML forms and do not themselves change stored data.
The update convention accepts both PUT and PATCH, although PATCH better communicates a partial modification. Rails route declarations can be limited with only or except, nested for parent-child resources, or extended with explicit member and collection routes. Those tools should describe a real resource relationship rather than mirror arbitrary method names.
A route existing does not guarantee a successful request. The target controller and public action must exist, constraints must accept the values, and downstream authorization may still deny access. Keeping dispatch separate from permission checks prevents the routes file from becoming hidden application logic.
Code Example
# config/routes.rb
Rails.application.routes.draw do
get '/health', to: 'health#show'
resources :articles, only: [:index, :show, :create] do
member do
post :publish
end
end
end
# GET /articles/7 -> ArticlesController#show
# params[:id] == '7'
# article_path(7) == '/articles/7'
Common Mistakes & Pitfalls
Hand-writing the 7 routes when resources :posts generates them, or forgetting root (no homepage).
Check your understanding
Answer these without looking back at the article.
- Which seven actions does `resources` generate?
- Why can route declaration order change which action receives a request?
- What is the difference between a member route and a collection route?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.