Routes and Resources
RESTful routing.
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"
endHTTP Verbs
Each route specifies a verb that signals intent:
getread data.postcreate data.patch/putupdate data.deleteremove 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
endThe Seven Actions
resources :articles creates routes for:
indexlist,showone item.newform,createsave.editform,updatesave.destroydelete.
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/commentsThe 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"
endInspecting 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:
memberroutes act on one record:/articles/1/publish.collectionroutes act on the set:/articles/search.
resources :articles do
member { post :publish }
collection { get :search }
endQuick Check
Test your understanding of routing.
Recap: Routes and Resources
You learned RESTful routing:
- Routes map verb plus path to
controller#action. resourcesgenerates seven standard routes.- Path helpers avoid hardcoded URLs.
only/except, nesting, member, and collection routes customize the set.rootsets the homepage;rails routeslists everything.
Next we write the controllers that handle these requests.
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
- Routes and Resources
- Controllers and Actions
- Strong Parameters
- Views and Rendering