Routes and Resources
RESTful routing.
Routes and Resources is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Routes and Resources” lesson free?
Yes — the full text of “Routes and Resources” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
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