Routing & RESTful Resources

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.

Learn Routing & RESTful Resources in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, 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

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.

Run rails routes to see exactly what resources :posts created.

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.

Your turn. Add the one line that gives comments all seven RESTful routes.

Given resources :books , name the verb and path for several operations, then verify with rails routes .

📋 Quick Reference — Routing

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.