Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Ruby on Rails - Understanding Routes in Rails

Introduction

Routing in Ruby on Rails is the process of mapping incoming web requests to the appropriate controller actions. The routes determine how URLs are matched with the controller actions and how data is passed between them.

Key Points:

  • Routes in Rails are defined in the config/routes.rb file.
  • Routes map URLs to controller actions and can include parameters.
  • This guide covers the basics of defining and using routes in Rails.

Defining Routes

Routes are defined in the config/routes.rb file using a domain-specific language provided by Rails. Here is a simple example of a route definition:

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

This defines a route that maps the URL /welcome/index to the index action in the WelcomeController.

Resourceful Routing

Rails provides a convenient way to define routes for RESTful resources using the resources method. This generates all the necessary routes for CRUD operations.

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

This generates the following routes:

  • GET /articles - index action
  • GET /articles/new - new action
  • POST /articles - create action
  • GET /articles/:id - show action
  • GET /articles/:id/edit - edit action
  • PATCH/PUT /articles/:id - update action
  • DELETE /articles/:id - destroy action

Named Routes

Named routes allow you to create custom URL helpers for your routes. This can make your code more readable and easier to maintain.

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

This creates a named route welcome_path that you can use in your views and controllers:

<%= link_to 'Welcome', welcome_path %>
                

Route Parameters

You can define routes with dynamic segments to capture parameters from the URL. These parameters are passed to the controller action.

# config/routes.rb
Rails.application.routes.draw do
  get 'articles/:id', to: 'articles#show'
end
                

In the controller, you can access the parameter using params[:id]:

# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  def show
    @article = Article.find(params[:id])
  end
end
                

Nested Routes

Nested routes allow you to express relationships between 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, which map to the CommentsController within the context of a specific article.

Conclusion

Understanding routing in Rails is crucial for developing robust and maintainable web applications. By defining routes in the config/routes.rb file, you can map URLs to controller actions and create a structured flow for your application.