Introduction
The Rails router is the traffic controller of your application. It examines every incoming URL and directs the request to the correct controller action, turning web addresses into code execution.
Key Concepts
- Router: The component that maps incoming URLs to controller actions, defined in
config/routes.rb. - RESTful resources:
resources :articlesgenerates 7 standard CRUD routes in one line. - Path helpers: Auto-generated methods like
articles_pathandarticle_path(@article)that produce URL strings. - Route parameters: Dynamic URL segments like
:idin/articles/:idthat become available inparams.
Real World Context
Clean, predictable URLs are essential for SEO and user experience. Rails' RESTful routing conventions mean every Rails developer knows that GET /articles/1 shows an article and DELETE /articles/1 deletes it, without reading any documentation.
Deep Dive
The Routes File
rubyRails.application.routes.draw do root "pages#home" resources :articles get "/about", to: "pages#about" end
RESTful Resource Routes
resources :articles generates:
| HTTP Verb | Path | Controller#Action | Purpose |
|---|---|---|---|
| GET | /articles | articles#index | List all |
| GET | /articles/new | articles#new | New form |
| POST | /articles | articles#create | Create |
| GET | /articles/:id | articles#show | Show one |
| GET | /articles/:id/edit | articles#edit | Edit form |
| PATCH/PUT | /articles/:id | articles#update | Update |
| DELETE | /articles/:id | articles#destroy | Delete |
Path and URL Helpers
rubyarticles_path # => "/articles" new_article_path # => "/articles/new" article_path(1) # => "/articles/1" edit_article_path(1) # => "/articles/1/edit" article_url(@article) # => "http://localhost:3000/articles/1"
Viewing Routes
bashbin/rails routes bin/rails routes -g articles # Filter by pattern
Common Pitfalls
- Route order matters: Routes are matched top-to-bottom. A catch-all route defined too early can swallow more specific routes.
- Forgetting
_pathvs_url: Use_pathfor links within your app and_urlfor emails and redirects that need full URLs. - Over-nesting resources: Deeply nested routes like
/users/1/posts/2/comments/3are hard to manage. Keep nesting to one level.
Best Practices
- Use
resourcesfor standard CRUD operations instead of defining routes manually. - Run
bin/rails routesfrequently to verify your routes are correct. - Use
_pathhelpers instead of hardcoded URL strings.
Summary
- All routes are defined in
config/routes.rb. resources :articlesgenerates 7 RESTful routes for CRUD operations.- Rails generates path helpers like
articles_pathandarticle_path(@article)automatically. - Use
bin/rails routesto inspect all defined routes. - Route parameters (
:id) are accessible via theparamshash in controllers.
Code Examples
ruby
# config/routes.rb
Rails.application.routes.draw do
root "pages#home"
resources :articles
get "/about", to: "pages#about"
end
# Generated helpers:
# articles_path => "/articles"
# article_path(1) => "/articles/1"
# new_article_path => "/articles/new"