Creating a simple CRUD application with Ruby on Rails

Creating a Simple CRUD Application with Ruby on Rails

Ruby on Rails, often simply referred to as Rails, is a powerful web application framework that allows developers to build applications quickly and efficiently. One of the most fundamental applications you can create with Rails is a CRUD (Create, Read, Update, Delete) application. In this article, we'll walk through the process of building a simple CRUD application using Ruby on Rails, complete with clear code examples and step-by-step instructions.

What is a CRUD Application?

A CRUD application is a type of software that allows users to perform four basic operations on a data set:

  • Create: Add new records to the database.
  • Read: Retrieve and display existing records.
  • Update: Modify existing records.
  • Delete: Remove records from the database.

CRUD applications are essential in web development, as they are the backbone of many business applications, content management systems, and social platforms.

Setting Up Your Rails Environment

Before building your CRUD application, you need to set up your development environment. Follow these steps:

  1. Install Ruby: If you haven't installed Ruby yet, visit the official Ruby website and follow the instructions for your operating system.

  2. Install Rails: Once Ruby is installed, you can install Rails by running the following command in your terminal: bash gem install rails

  3. Create a New Rails Application: Create a new Rails project by executing: bash rails new crud_app cd crud_app

  4. Set Up the Database: Rails uses SQLite by default, which is perfect for development. You can create the database by running: bash rails db:create

Generating a Scaffold

Rails makes it easy to create a CRUD interface using scaffolding. A scaffold generates a model, views, and controller for you. Let’s create a simple application that manages a list of books.

  1. Generate a Scaffold for Books: Run the following command to create a scaffold for a Book model with attributes title and author: bash rails generate scaffold Book title:string author:string

  2. Run Migrations: After generating the scaffold, run the migrations to create the database table: bash rails db:migrate

Understanding the Generated Code

The scaffold command creates several files and directories that are crucial for your application:

  • Model: Located in app/models/book.rb, this file defines the data structure and relationships.
  • Controller: Found in app/controllers/books_controller.rb, it handles the CRUD operations.
  • Views: Located in app/views/books/, these files are responsible for rendering the HTML.

Reading and Understanding the Books Controller

The BooksController includes methods for each CRUD operation. Here’s a brief overview:

  • index: Displays a list of all books.
  • show: Displays a single book.
  • new: Renders a form for creating a new book.
  • create: Saves the new book to the database.
  • edit: Renders a form for editing an existing book.
  • update: Updates the book record in the database.
  • destroy: Deletes a book from the database.

Running the Application

To see your CRUD application in action, start the Rails server:

rails server

Now, open your web browser and navigate to http://localhost:3000/books. You should see the interface for managing books.

Basic CRUD Operations

  • Create: Click on "New Book", fill in the details, and click "Create Book".
  • Read: You will be redirected to the index page where you can see all books listed.
  • Update: Click on the "Edit" link next to a book, modify the details, and click "Update Book".
  • Delete: Click on the "Destroy" link next to a book to remove it from the database.

Customizing Your Application

Adding Validations

To ensure data integrity, you can add validations to your Book model. Open app/models/book.rb and add the following code:

class Book < ApplicationRecord
  validates :title, presence: true
  validates :author, presence: true
end

This code ensures that both title and author must be provided when creating or updating a book.

Enhancing the User Interface

You can enhance the user interface by using Bootstrap or another CSS framework. To add Bootstrap, include the following line in your Gemfile:

gem 'bootstrap', '~> 4.1.0'

Run bundle install and then modify your application layout in app/views/layouts/application.html.erb to include Bootstrap's CSS.

Troubleshooting Common Issues

As you develop your application, you might encounter some common issues:

  • Database Errors: Double-check that your migrations have run successfully. You can check the schema in db/schema.rb.
  • Routing Issues: Ensure that your routes are correctly set up by checking config/routes.rb. The scaffold command should have automatically added the necessary routes.

Conclusion

Creating a simple CRUD application with Ruby on Rails is an excellent way for beginners to understand the framework's capabilities. By following the steps outlined in this guide, you've built a fully functional application that allows users to create, read, update, and delete records.

As you continue to explore Ruby on Rails, consider adding more features like user authentication, search functionality, or even integrating an API. The possibilities are endless, and with Rails, you have a powerful tool at your fingertips to bring your ideas to life!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.