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
- Routes and Paths: Routes define the mapping between URLs and controller actions.
- HTTP Verbs: Rails routes can respond to different HTTP verbs (GET, POST, PUT, DELETE, etc.).
- Named Routes: Named routes provide a way to generate paths and URLs using route helpers.
- Resourceful Routing: A convenient way to define routes for a resource, following RESTful conventions.
- 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:
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:
You can use the named route helper in your views and controllers:
Resourceful Routing
Resourceful routing provides a convenient way to define routes for a resource, following RESTful conventions. Here’s an example:
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
- Create a new Rails application.
- Generate a
Posts
controller with actions:index
,show
,new
,create
,edit
,update
, anddestroy
. - Define resourceful routes for
posts
. - Create views for each action in the
Posts
controller. - Test the routes by navigating to the URLs in your browser.
Solution
- Create a new Rails application:
- Generate the
Posts
controller:
- Define resourceful routes:
- 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>
- 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.
Ruby Programming Course
Module 1: Introduction to Ruby
Module 2: Basic Ruby Concepts
Module 3: Working with Collections
Module 4: Object-Oriented Programming in Ruby
- Classes and Objects
- Instance Variables and Methods
- Class Variables and Methods
- Inheritance
- Modules and Mixins
Module 5: Advanced Ruby Concepts
Module 6: Ruby on Rails Introduction
- What is Ruby on Rails?
- Setting Up Rails Environment
- Creating a Simple Rails Application
- MVC Architecture
- Routing
Module 7: Testing in Ruby
- Introduction to Testing
- Unit Testing with Minitest
- Behavior-Driven Development with RSpec
- Mocking and Stubbing