Ruby Academy · Lesson

Routes and Resources

RESTful routing.

Lesson 1 of 413 steps

Routes and Resources is a free Ruby Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

What Is Routing?

Routing maps an incoming HTTP request (a method plus a path) to a controller action that handles it. The routes file is the front door of a Rails app.

  • Routes live in config/routes.rb.
  • Each route connects a URL to controller#action.
  • Rails matches routes top to bottom.

A Basic Route

The simplest route maps a path to an action with get. Here a GET to /about calls the about action on PagesController.

Rails.application.routes.draw do
  get "/about", to: "pages#about"
end

HTTP Verbs

Each route specifies a verb that signals intent:

  • get read data.
  • post create data.
  • patch / put update data.
  • delete remove data.

The same path with different verbs can map to different actions.

RESTful Resources

The resources helper generates the seven standard REST routes for a model in one line. This is the heart of conventional Rails routing.

Rails.application.routes.draw do
  resources :articles
end

The Seven Actions

resources :articles creates routes for:

  • index list, show one item.
  • new form, create save.
  • edit form, update save.
  • destroy delete.

This covers the full lifecycle of a resource.

Path Helpers

Routes generate named helpers so you never hardcode URLs. resources :articles gives articles_path, article_path(@article), new_article_path, and edit_article_path(@article).

articles_path            # => "/articles"
article_path(5)          # => "/articles/5"
new_article_path         # => "/articles/new"
edit_article_path(5)     # => "/articles/5/edit"

Limiting Actions

Use only or except to generate just the routes you need. This keeps your routing surface small and intentional.

resources :articles, only: [:index, :show]
resources :comments, except: [:edit, :update]

Nested Resources

Nest resources to express ownership in the URL. Comments belonging to an article become /articles/1/comments. The nested route passes article_id to the controller.

resources :articles do
  resources :comments
end

# GET /articles/1/comments

The Root Route

root defines what loads at /, the homepage of your app. Place it near the top of the routes file for clarity.

Rails.application.routes.draw do
  root "articles#index"
end

Inspecting Routes

Run rails routes to list every route with its verb, path, helper, and controller#action. This is the fastest way to understand or debug an app's routing.

Member and Collection Routes

Add custom actions to a resource:

  • member routes act on one record: /articles/1/publish.
  • collection routes act on the set: /articles/search.
resources :articles do
  member { post :publish }
  collection { get :search }
end

Quick Check

Test your understanding of routing.

Recap: Routes and Resources

You learned RESTful routing:

  • Routes map verb plus path to controller#action.
  • resources generates seven standard routes.
  • Path helpers avoid hardcoded URLs.
  • only/except, nesting, member, and collection routes customize the set.
  • root sets the homepage; rails routes lists everything.

Next we write the controllers that handle these requests.

Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Routes and Resources” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Routes and Resources”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “Routes and Resources”?

RESTful routing. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Routes and Resources” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Routes and Resources
  2. Controllers and Actions
  3. Strong Parameters
  4. Views and Rendering
← Back to Ruby Academy