Chapter 4: Routing in Rails

Chapter 4: Routing in Rails

AI Reading

Quick summary of this article

Routing in Rails determines which controller action handles a given URL, and all routes are defined in the config/routes.rb file. Each route follows a simple pattern: an HTTP verb, a URL path, and a controller#action pair. Rails also provides a powerful resources method that automatically creates all standard RESTful routes for a resource with a single line of code.

  • Basic route syntax: verb "/path", to: "controller#action" — for example, get "/welcome", to: "pages#welcome" sends visitors of /welcome to the welcome action in PagesController.
  • HTTP verbs map to actions: get for reading, post for creating, patch/put for updating, and delete for removing data.
  • The resources :posts method generates seven RESTful routes, including paths for listing posts, showing a single post, creating new posts, editing, updating, and deleting.
  • Use the command rails routes to view a complete list of all routes defined in your application.
  • Example practice: add get "/home", to: "pages#home" for a homepage, use resources :posts for post routes, and run rails routes to verify them.

Chapter 4: Routing in Rails

Routing is how Rails decides which controller action should handle a URL. All routes are defined in a single file: config/routes.rb. Understanding routes is the first step to controlling what your application does.

In this chapter you will learn the basic route syntax, how to create simple routes, and how the resources method generates RESTful routes automatically.

🔗 Basic Route Syntax

Every route has the format verb "/path", to: "controller#action". The part before the # is the controller name and the part after it is the action (a method) inside that controller.

# config/routes.rb
get "/welcome", to: "pages#welcome"

This tells Rails: when a user visits /welcome, run the welcome action of the PagesController.

📑 Common HTTP Verbs

  • get – used to request a page (reading).
  • post – used to create something (form submit).
  • patch/put – used to update something.
  • delete – used to remove something.
# config/routes.rb
get    "/about", to: "pages#about"
post   "/contact", to: "contacts#create"
patch  "/posts/:id", to: "posts#update"
delete "/posts/:id", to: "posts#destroy"

🔧 The resources Method

For a resource like posts, Rails provides the resources method, which creates all the standard RESTful routes with one line:

# config/routes.rb
resources :posts

This single line generates: GET /posts, GET /posts/new, POST /posts, GET /posts/:id, GET /posts/:id/edit, PATCH/PUT /posts/:id, and DELETE /posts/:id.

🔍 Seeing Your Routes

Rails has a built-in command that lists all the routes in your application:

rails routes

🏠 Practice Exercises

# 1. Add a simple route for a home page
get "/home", to: "pages#home"

# 2. Use resources for posts
resources :posts

# 3. View all routes
rails routes

In the next chapter you will learn how controllers and views turn these routes into actual pages.

Leave a Reply

Your email address will not be published. Required fields are marked *