Skip to content

Implementing CRUD in Ruby: A Step-by-Step Guide

To implement CRUD (Create, Read, Update, Delete) functionality in a Ruby web application, there are a few key steps that need to be taken. Here is a complete guide on how to implement CRUD in Ruby:

Step 1: Define Your Routes

First, you need to define your routes in your application's config/routes.rb file. This will allow your application to receive HTTP requests and route them to the appropriate controller action. For example:

rb
Rails.application.routes.draw do
resources :articles
end

This defines a set of RESTful routes for the Article model, including GET /articles, GET /articles/:id, POST /articles, PUT /articles/:id, and DELETE /articles/:id.

Step 2: Define Your Model

Next, you need to define your model in app/models/article.rb. This will define the structure of your data and how it interacts with the database. For example:

rb
class Article < ApplicationRecord
validates :title, presence: true
validates :body, presence: true
end

This defines an Article model with title and body attributes, and validates that both are present.

Step 3: Define Your Controller

Next, you need to define your controller in app/controllers/articles_controller.rb. This will define the logic for each of your CRUD actions. For example:

rb
class ArticlesController < ApplicationController
def index
@articles = Article.all
end

def show
@article = Article.find(params[:id])
end

def new
@article = Article.new
end

def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end

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

def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end

private

def article_params
params.require(:article).permit(:title, :body)
end
end

This defines an ArticlesController with actions for index, show, new, create, edit, update, and destroy. The new, create, edit, and update actions use a form to submit data to the server, while the destroy action uses a link to send a DELETE request.

Step 4: Define Your Views

Finally, you need to define your views in app/views/articles/. This will define the HTML that is displayed to the user. For example:

html
<!-- app/views/articles/index.html.erb -->
<h1>Articles</h1>

<ul>
	<% @articles.each do |article| %>
	<li><%= link_to article.title, article_path(article) %></li>
	<% end %>
</ul>

This defines a view for the index action that displays a list of all articles. With these steps, you should now have a fully functional CRUD implementation in your Ruby web application!