Routing is a crucial aspect of any web application, as it determines how incoming requests are handled and directed to the appropriate controller actions. In Ruby on Rails, routing is managed through the config/routes.rb file. This file defines the paths that map to various controller actions, enabling the application to respond to different URLs.

Key Concepts

  1. Routes and Paths: Routes define the mapping between URLs and controller actions.
  2. HTTP Verbs: Rails routes can respond to different HTTP verbs (GET, POST, PUT, DELETE, etc.).
  3. Named Routes: Named routes provide a way to generate paths and URLs using route helpers.
  4. Resourceful Routing: A convenient way to define routes for a resource, following RESTful conventions.
  5. Nested Routes: Routes that are nested within other routes, often used for hierarchical resources.

Basic Routing

In Rails, you define routes in the config/routes.rb file. Here’s a simple example:

# config/routes.rb
Rails.application.routes.draw do
  get 'welcome/index'
end

This route maps the URL /welcome/index to the index action of the WelcomeController.

Example

# app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
  def index
    render plain: "Hello, Rails!"
  end
end

When you navigate to http://localhost:3000/welcome/index, you will see "Hello, Rails!" displayed in your browser.

HTTP Verbs

Rails routes can respond to different HTTP verbs. Here’s how you can define routes for different actions:

# config/routes.rb
Rails.application.routes.draw do
  get 'articles', to: 'articles#index'
  post 'articles', to: 'articles#create'
  get 'articles/new', to: 'articles#new'
  get 'articles/:id', to: 'articles#show'
  put 'articles/:id', to: 'articles#update'
  delete 'articles/:id', to: 'articles#destroy'
end

Named Routes

Named routes allow you to generate paths and URLs using route helpers. Here’s an example:

# config/routes.rb
Rails.application.routes.draw do
  get 'welcome/index', as: 'welcome'
end

You can use the named route helper in your views and controllers:

<!-- app/views/layouts/application.html.erb -->
<%= link_to 'Welcome', welcome_path %>

Resourceful Routing

Resourceful routing provides a convenient way to define routes for a resource, following RESTful conventions. Here’s an example:

# config/routes.rb
Rails.application.routes.draw do
  resources :articles
end

This single line generates all the necessary routes for CRUD operations on the articles resource:

HTTP Verb Path Controller#Action Used for
GET /articles articles#index Display a list of all articles
GET /articles/new articles#new Return an HTML form for creating a new article
POST /articles articles#create Create a new article
GET /articles/:id articles#show Display a specific article
GET /articles/:id/edit articles#edit Return an HTML form for editing an article
PATCH/PUT /articles/:id articles#update Update a specific article
DELETE /articles/:id articles#destroy Delete a specific article

Nested Routes

Nested routes are used for hierarchical resources. For example, if you have comments that belong to articles, you can nest the routes:

# config/routes.rb
Rails.application.routes.draw do
  resources :articles do
    resources :comments
  end
end

This generates routes like /articles/:article_id/comments/:id.

Practical Exercise

Task

  1. Create a new Rails application.
  2. Generate a Posts controller with actions: index, show, new, create, edit, update, and destroy.
  3. Define resourceful routes for posts.
  4. Create views for each action in the Posts controller.
  5. Test the routes by navigating to the URLs in your browser.

Solution

  1. Create a new Rails application:
rails new blog
cd blog
  1. Generate the Posts controller:
rails generate controller Posts index show new edit
  1. Define resourceful routes:
# config/routes.rb
Rails.application.routes.draw do
  resources :posts
end
  1. Create views for each action:
<!-- app/views/posts/index.html.erb -->
<h1>All Posts</h1>

<!-- app/views/posts/show.html.erb -->
<h1>Show Post</h1>

<!-- app/views/posts/new.html.erb -->
<h1>New Post</h1>

<!-- app/views/posts/edit.html.erb -->
<h1>Edit Post</h1>
  1. Test the routes by navigating to:
  • http://localhost:3000/posts
  • http://localhost:3000/posts/new
  • http://localhost:3000/posts/:id
  • http://localhost:3000/posts/:id/edit

Conclusion

Routing is a fundamental aspect of Rails applications, enabling you to map URLs to controller actions efficiently. By understanding and utilizing basic routing, HTTP verbs, named routes, resourceful routing, and nested routes, you can create a well-structured and navigable web application. In the next module, we will delve into testing in Ruby, ensuring that your application is robust and reliable.

© Copyright 2024. All rights reserved