RESTful routing in Rails

RESTful Routing in Rails for CRUD Operations

RESTful routing is a fundamental concept in Ruby on Rails that simplifies the implementation of CRUD (Create, Read, Update, Delete) operations in web applications. In this article, we’ll dive into RESTful routing in Rails with practical code examples to help you understand and implement it effectively in your projects.

Understanding RESTful Routing in Rails

RESTful routing is based on the idea that web applications should follow a set of conventions for handling different types of requests. These conventions map HTTP verbs (GET, POST, PUT, DELETE) to controller actions and make your application’s behavior more predictable and consistent.

1. Creating a Resource

Let’s start by creating a simple resource, say, “articles.”

# Terminal
rails generate scaffold Article title:string body:text

This command generates a new resource, including a controller (ArticlesController) and views for CRUD operations.

2. Index and Show Actions

The index action lists all articles, and the show action displays a specific article.

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

3. New and Create Actions

The new action displays a form for creating a new article, while the create action handles the form submission and creates a new record in the database.

# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
 # …
def new
  @article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
  redirect_to @article
else
render ‘new’
 end
end
 private
def article_params
params.require(:article).permit(:title, :body)
 end
end

4. Edit and Update Actions

The edit action displays a form for updating an existing article, and the update action handles the form submission and updates the article record.

# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  # …
  def edit
    @article = Article.find(params[:id])
  end
  def update
   @article = Article.find(params[:id])
    if @article.update(article_params)
      redirect_to @article
    else
      render ‘edit’
    end
  end
   # …
end

5. Destroy Action

The destroy action deletes an article from the database.

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

6. Routes Configuration

In your config/routes.rb file, Rails automatically generates RESTful routes for your resource.

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

With these routes, your Rails application now supports all the standard CRUD operations for articles.

Conclusion

RESTful routing in Rails provides a structured and efficient way to handle CRUD operations in your web applications. By following these conventions and using code examples like the ones provided in this article, you can quickly create powerful and maintainable web applications with Ruby on Rails.


Related Posts

Leave a Comment

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

en_USEnglish