RoR Routes and URL Mapping
Routes connect URLs to your application code. When a browser sends a request to your Rails app, the router reads the URL and decides which controller and action should handle it. Without routes, your app has no way to respond to any request.
Where Routes Live
All routes go in one file: config/routes.rb. Rails reads this file on every request and matches the URL to the correct destination.
How Routing Works
Browser sends: GET /articles
config/routes.rb reads every rule top to bottom
|
v
Finds a match: get "/articles", to: "articles#index"
|
v
Sends request to: ArticlesController → index action
|
v
Controller runs, view renders, browser gets HTML
Basic Route Syntax
A route has three parts: the HTTP verb, the URL path, and the destination.
get "/articles", to: "articles#index" ^ ^ ^ verb path controller#action
The four HTTP verbs Rails uses:
| Verb | Purpose | Example URL |
|---|---|---|
| GET | Read / display something | /articles |
| POST | Create something new | /articles |
| PATCH / PUT | Update something existing | /articles/1 |
| DELETE | Remove something | /articles/1 |
The Root Route
The root route defines your homepage — the page users see at /.
root "articles#index"
This sends anyone visiting http://localhost:3000 to the index action in ArticlesController.
Resourceful Routes
Rails provides a shortcut that generates all seven standard routes for a resource in one line:
resources :articles
This single line creates seven routes:
Verb URL Controller#Action Purpose -------------------------------------------------------------- GET /articles articles#index List all GET /articles/new articles#new Show new form POST /articles articles#create Save new record GET /articles/:id articles#show Show one GET /articles/:id/edit articles#edit Show edit form PATCH /articles/:id articles#update Save changes DELETE /articles/:id articles#destroy Delete record
The :id in the URL is a dynamic segment. It captures whatever number appears there and makes it available in the controller as params[:id].
URL Helpers
When you use resources, Rails automatically generates helper methods for each route. These helpers produce the correct URL path so you never hardcode URLs in your views.
Route helper Generates URL ------------------------------------------- articles_path /articles new_article_path /articles/new article_path(@article) /articles/3 edit_article_path(@a) /articles/3/edit
Use them in views like this:
<a href="<%= articles_path %>">All Articles</a> <a href="<%= new_article_path %>">Write New Article</a> <a href="<%= article_path(@article) %>">Read This Article</a>
Named Routes
You can give any route a name using the as option:
get "/about", to: "pages#about", as: "about" get "/contact", to: "pages#contact", as: "contact"
Now you can use about_path and contact_path in your views instead of typing the URL manually.
Route Parameters
Dynamic segments capture values from the URL:
get "/users/:id", to: "users#show" User visits: /users/42 Controller receives: params[:id] = "42"
You can use any name for the segment:
get "/products/:category/:id", to: "products#show" User visits: /products/electronics/7 params[:category] = "electronics" params[:id] = "7"
Limit Which Routes Are Generated
If your resource only needs some of the seven routes, limit them with only or except:
resources :articles, only: [:index, :show] resources :comments, except: [:destroy]
This keeps your route table clean and prevents exposing actions that do not exist yet.
Check All Your Routes
Run this command to see every route your app currently has:
rails routes
Filter routes by name to find a specific one:
rails routes | grep article
The Routes Diagram
config/routes.rb
|
+-- root "pages#home"
|
+-- resources :articles
| |
| +-- GET /articles → articles#index
| +-- GET /articles/new → articles#new
| +-- POST /articles → articles#create
| +-- GET /articles/:id → articles#show
| +-- GET /articles/:id/edit → articles#edit
| +-- PATCH /articles/:id → articles#update
| +-- DELETE /articles/:id → articles#destroy
|
+-- get "/about", to: "pages#about", as: "about"
Redirect Routes
You can redirect one URL to another directly inside routes:
get "/home", to: redirect("/")
get "/blog", to: redirect("/articles")
When a user visits /home, Rails immediately redirects them to /. No controller needed.
Scoped Routes
Group routes under a shared path prefix using scope:
scope "/admin" do resources :users resources :posts end
This generates URLs like /admin/users and /admin/posts while keeping the same controller names.
Routes are the entry points to your entire application. Getting comfortable with them makes everything else in Rails easier to understand and build.
