RESTful resource routing in Rails
Fast recall
Quick revision
Core answer
Whether the learner can map method, collection/member path, action, and helper without memorizing URLs in isolation.
Using verbs in resource paths or confusing new/edit form routes with create/update operations.
Explain member versus collection, only/except, nesting limits, and route inspection.
Interview-ready guide
Interview answer
Rails resource routing treats a model-like concept as a resource and maps standard HTTP operations to conventional controller actions. The router uses method plus path, so GET /articles/7 reaches show while PATCH /articles/7 reaches update.
resources :articles creates seven routes in a full application: index, show, new, create, edit, update, and destroy. new and edit serve HTML forms; create and update receive the submitted changes. The same declaration also creates named path and URL helpers.
For example, a JSON API can declare resources :articles, only: [:index, :show, :create, :update, :destroy]. It keeps collection reads and writes plus member reads, updates, and deletes without publishing form-page routes that the API does not serve.
Extra operations should be classified carefully. A preview for one article is a member route; a search over all articles is a collection route. Deeply nested resources create long URLs and coupled controllers, so I usually nest only when the parent identity is truly required and limit nesting depth.
I verify the result with bin/rails routes, including verb, path, controller action, parameter names, and helper. Resourceful routing is valuable because it makes the HTTP contract predictable, not because every controller must expose all seven actions. I then cover the published routes with request specs so an accidental route change is caught before a client receives an unexpected 404 or reaches the wrong action.
RESTful resource routing in Rails
Direct Answer
Rails resource routing maps HTTP method plus resource-shaped path to conventional controller actions and path helpers. resources :articles creates seven full-stack routes; an API usually limits that set to actions it exposes and omits the new and edit form routes.
Deep Dive & Architecture
A collection path has no record identifier. GET /articles lists the collection through index, while POST /articles submits a new representation to create. A member path includes :id. GET /articles/:id reads one record through show, PATCH or PUT sends changes to update, and DELETE requests destroy.
The remaining two routes exist to support server-rendered forms. GET /articles/new reaches new and displays a creation form. GET /articles/:id/edit reaches edit and displays an update form. They do not modify data. A JSON API with an independent client normally omits them using only, while a full Rails application can use all seven.
Route order matters because Rails chooses the first matching route. A custom path such as /articles/search placed after a broad member route may be interpreted as an ID in some route shapes. Prefer resourceful member or collection declarations, meaningful constraints, and bin/rails routes --expanded when diagnosing an unexpected match.
Nesting communicates ownership in the URL: /magazines/3/ads/8 says the ad is addressed through magazine 3. It can also duplicate information and force every child action to load a parent. Shallow routes or top-level resources are often clearer after one level. Use member routes for one existing resource and collection routes for operations over the set.
Routes are not authorization. A matching route only decides which code receives the request. The controller must still authenticate the caller, authorize the record or collection, validate parameters, and choose the response. Request specs should cover both routability and behaviour, especially when only, namespaces, version prefixes, or constraints define the public API surface. URL helpers should be preferred over hand-built strings because they follow the declared host, namespace, and parameter encoding rules.
When a command does not fit CRUD cleanly, first ask whether it creates a new resource or changes an existing one's state. A cancellation may be a state update; a long-running export may create an export resource. This often produces clearer retries, statuses, and audit history than an arbitrary verb endpoint.
Code Example
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :articles, only: [:index, :show, :create, :update, :destroy] do
get :preview, on: :member
get :search, on: :collection
end
end
end
end
The API omits form routes, then labels preview as member work and search as collection work.
Common Mistakes & Pitfalls
Using verbs in resource paths or confusing new/edit form routes with create/update operations.
Check your understanding
Answer these without looking back at the article.
- Which seven routes does resources create by default?
- What is the difference between a member and collection route?
- Why should route nesting usually remain shallow?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.