Routing & RESTful Resources
Reviewed & published by Brayan K
Routing is how Ruby on Rails connects an incoming URL to the controller action that handles it — and Rails embraces REST, a convention that maps standard HTTP verbs to standard CRUD operations.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.
By the end of this lesson you'll write routes in config/routes.rb, use resources to generate RESTful routes, read the routes table, and use route helpers.
What You'll Learn in This Lesson
- Declaring routes in config/routes.rb
- The resources :posts macro
- The seven RESTful routes
- HTTP verbs and CRUD actions
- Route helpers like posts_path
- Listing routes with rails routes
1️⃣ Declaring Routes and resources
Routes live in config/routes.rb. You can write single routes by hand, but the resources :posts macro generates all seven RESTful routes at once.
# config/routes.rb — the map from URLs to controller actions.
Rails.application.routes.draw do
# The home page (/).
root "pages#home"
# A single explicit route: GET /about -> PagesController#about
get "/about", to: "pages#about"
# ONE line that creates all seven RESTful routes for posts:
resources :posts
endRun rails routes to see exactly what resources :posts created.
# Run this in your terminal to list the routes:
rails routes
# The seven routes that resources :posts generates:2️⃣ Route Helpers
Every resource also gives you named helpers that build URLs for you. Prefer them over hardcoding paths, so your links keep working if the URLs ever change.
# Route HELPERS give you names instead of hardcoded URLs.
# resources :posts also defines these helpers:
posts_path # -> "/posts" (index / create)
new_post_path # -> "/posts/new" (new form)
edit_post_path(@p) # -> "/posts/5/edit" (edit form)
post_path(@p) # -> "/posts/5" (show / update / destroy)
# Use them in views and controllers instead of typing URLs:
# link_to "All posts", posts_path
# redirect_to post_path(@post)
# Add _url instead of _path for a full URL with the host.Your turn. Add the one line that gives comments all seven RESTful routes.
# 🎯 YOUR TURN — write the routes for a "comments" resource.
# Edit config/routes.rb.
Rails.application.routes.draw do
root "pages#home"
# 1) Add ONE line that creates all 7 RESTful routes for comments.
______ :comments # 👉 TODO: the resources macro
end
# ✅ After adding it, rails routes should list:
# comments#index, create, new, edit, show, update, destroyMini-Challenge: Read the Routes
Given resources :books, name the verb and path for several operations, then verify with rails routes.
# 🎯 MINI-CHALLENGE: read the routes table.
# Save your answers and check with: rails routes
#
# Given resources :books is in routes.rb, answer:
# 1. Which verb+path LISTS all books?
# 2. Which verb+path SHOWS book number 7?
# 3. Which verb+path DELETES a book?
# 4. What does books_path return?
#
# ✅ Example answers:
# GET /books
# GET /books/7
# DELETE /books/7
# "/books"
# your answers hereCommon Errors (and the fix)
- Singular resource name — Use plural: resources :posts, not resources :post.
- "No route matches" — The verb or path doesn't exist; check rails routes for the correct one.
- Hardcoding URLs — Prefer helpers like posts_path over typing "/posts".
- Missing controller/action — A route can exist with no matching action, causing an error; create the action.
- Forgetting root — Without root, visiting / shows the default page, not yours.
Pro Tips
- 💡 Prefer resources: it keeps your routes RESTful and concise.
- 💡 Run rails routes often: it's the fastest way to see what's wired up.
- 💡 Use helpers everywhere: post_path(@post) beats string URLs.
📋 Quick Reference — Routing
| Concept | Syntax |
|---|---|
| All RESTful routes | resources :posts |
| Single route | get "/about", to: "pages#about" |
| Home page | root "pages#home" |
| Index helper | posts_path |
| Show helper | post_path(@post) |
| List routes | rails routes |
🎉 Lesson Complete!
- ✅ Routes live in config/routes.rb
- ✅ resources :posts creates seven RESTful routes
- ✅ The HTTP verb and path together choose the action
- ✅ Route helpers like post_path build URLs
- ✅ rails routes lists everything
Practice quiz
Which file holds your application's routes?
- app/routes.rb
- config/routes.rb
- config/urls.rb
- db/routes.rb
Answer: config/routes.rb. All routing rules are declared in config/routes.rb.
What does resources :posts generate?
- Only a single show route
- The seven standard RESTful routes for posts
- Just a database table
- A new posts controller file only
Answer: The seven standard RESTful routes for posts. resources :posts creates the seven conventional CRUD routes (index, show, new, create, edit, update, destroy).
Which HTTP verb does the RESTful route to CREATE a new post use?
- POST
- GET
- PUT
- DELETE
Answer: POST. Creating a record uses POST /posts, mapped to the create action.
Which command prints a table of all the app's routes?
- rails server
- rails console
- rails routes
- rails new
Answer: rails routes. rails routes lists every route with its verb, path, and controller#action.
The route GET /posts/:id maps to which controller action?
- index
- new
- create
- show
Answer: show. GET /posts/:id is the show action, displaying one specific post.
What does the route helper posts_path return?
- The /posts URL path as a string
- A database query
- The Post model class
- An array of post objects
Answer: The /posts URL path as a string. posts_path returns the path string "/posts", letting you avoid hardcoding URLs.
Which DELETE route removes a post, and to which action?
- DELETE /posts -> index
- DELETE /posts/:id -> destroy
- GET /posts/:id/delete -> remove
- POST /posts/:id -> destroy
Answer: DELETE /posts/:id -> destroy. DELETE /posts/:id is mapped to the destroy action.
How do you declare the home page route in routes.rb?
- home "pages#index"
- get "/"
- root "pages#home"
- index "pages#home"
Answer: root "pages#home". root "pages#home" sets the application's root (/) to that controller action.
Which RESTful route shows the form for editing an existing post?
- GET /posts/new -> new
- GET /posts/:id/edit -> edit
- PATCH /posts/:id -> update
- GET /posts -> index
Answer: GET /posts/:id/edit -> edit. GET /posts/:id/edit maps to the edit action, which renders the edit form.
The named helper post_path(@post) is most useful because it:
- Runs the database migration
- Creates a new controller
- Builds the correct URL for that specific post without hardcoding it
- Defines a new route
Answer: Builds the correct URL for that specific post without hardcoding it. post_path(@post) generates that post's URL (e.g. /posts/5), so links and redirects stay correct.
Continue this course
- Previous: Rails Project Structure & MVC
- Next: Controllers & Actions — Actions, params, render/redirect_to and before_action filters
- Quick reference: Ruby cheat sheet
Frequently asked questions
What does resources :posts actually do?
It is a single macro in config/routes.rb that generates the seven conventional RESTful routes for a resource: index (list all), show (view one), new (form to create), create (save the new record), edit (form to change), update (save changes), and destroy (delete). Each maps a specific HTTP verb and path to a matching action in PostsController. Instead of writing seven separate route lines, resources :posts gives you all of them — plus named route helpers — in one line, which is the convention Rails strongly encourages.
Why are there both GET and POST/PATCH/DELETE versions of paths?
REST uses the HTTP verb, not just the path, to express intent. GET /posts and POST /posts share a path but mean different things: GET lists posts (index), while POST submits a new one (create). Likewise /posts/:id is GET for show, PATCH for update, and DELETE for destroy. This is why the routes table shows the verb alongside the path — the same URL can route to different actions depending on the method. Browsers send GET for links and POST for forms; Rails forms include a hidden field to simulate PATCH and DELETE.
What are route helpers and why use them?
Route helpers are named methods like posts_path, new_post_path, and post_path(@post) that build URLs for you. resources :posts defines them automatically. Using posts_path instead of typing "/posts" means if the URL structure ever changes, your links and redirects update in one place. The _path versions give a relative path ("/posts/5") and the _url versions give a full URL including the host. You'll use them constantly in views (link_to "Show", post_path(post)) and controllers (redirect_to post_path(@post)).
How do I see all the routes my app has?
Run rails routes in your terminal from inside the project. It prints a table with four columns: the route name (helper prefix), the HTTP verb, the URL pattern, and the controller#action it maps to. This is the single best way to understand what URLs your app responds to and where each one goes. You can filter it, for example rails routes -g posts to show only routes matching 'posts'. Visiting a bad URL in development also shows a routing error page that lists the available routes.