DEV Community

Cover image for Building a CRUD Application with Ruby on Rails: Step-by-Step Guide
mhcrocky
mhcrocky

Posted on

Building a CRUD Application with Ruby on Rails: Step-by-Step Guide

Let’s take a deep dive into building a fully functional CRUD (Create, Read, Update, Delete) application using Ruby on Rails. I will walk through each step of the process, providing technical examples and explanations to help you understand the concepts and implementation details. By the end of this guide, you will have a solid foundation in developing CRUD applications with Ruby on Rails.

Step 1: Install Ruby on Rails
Make sure you have Ruby and Rails installed on your system. You can install Rails using the following command:

gem install rails
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a New Rails Application
Create a new Rails application using the following command:

rails new crud_app
cd crud_app
Enter fullscreen mode Exit fullscreen mode

Step 3: Generate a Model

Generate a model for the application. In this example, we’ll create a Task model with a title and description.

rails generate model Task title:string description:text
rake db:migrate
Enter fullscreen mode Exit fullscreen mode

Step 4: Generate a Controller
Generate a controller to handle the CRUD operations for the Task model.

rails generate controller Tasks
Enter fullscreen mode Exit fullscreen mode

Step 5: Define Routes
Open the config/routes.rb file and define the routes for the Tasks controller:

config/routes.rb

Rails.application.routes.draw do
resources :tasks
root 'tasks#index'
end
Enter fullscreen mode Exit fullscreen mode

Step 6: Implement Controller Actions

Open the app/controllers/tasks_controller.rb file and implement the CRUD actions:

app/controllers/tasks_controller.rb

class TasksController < ApplicationController
    before_action :set_task, only: [:show, :edit, :update, :destroy]
    def index
        @tasks = Task.all
    end

    def show
    end

    def new
        @task = Task.new
    end

    def create
        @task = Task.new(task_params)
        if @task.save
            redirect_to @task, notice: 'Task was successfully created.'
        else
            render :new
        end
    end

    def edit
    end

    def update
        if @task.update(task_params)
            redirect_to @task, notice: 'Task was successfully updated.'
        else
            render :edit
        end
    end

    def destroy
        @task.destroy
        redirect_to tasks_url, notice: 'Task was successfully destroyed.'
    end

    private
    def set_task
        @task = Task.find(params[:id])
    end

    def task_params
        params.require(:task).permit(:title, :description)
    end
end
Enter fullscreen mode Exit fullscreen mode

Step 7: Create Views
Create views for the Tasks controller. You can use the default Rails scaffold views or customize them based on your needs.

Step 8: Run the Application
Start the Rails server and navigate to http://localhost:3000 to see your CRUD application in action.

rails server
Enter fullscreen mode Exit fullscreen mode

That’s it! You’ve now created a simple CRUD application with Ruby on Rails. You can further customize and enhance it based on your requirements.

Thanks for reading
Want to work together? Contact me on Telegram

I am an freelancer have much much experience in React.js. Want to catch the journey? Follow me on github.

Top comments (0)