How to perform CRUD functionalities on Rails

Rails CRUD processes is very easy to create, this makes it easy to work on something else. Compare to Django CRUD operation, I can say for Django you need to have sort of a hacking mentality to go through it, but for Rails its just one command and everything is created on a fly.

Related Articles

1. Creating a Rails project

To create a new Rails project template, create a directory where you want to store your project. Cd into that project and start the new Rails project with the following command.

rails new <project_name>

Wait for it to create a project template for you, if you run into problems its mostly because of the sqlite3 database. One way or the other you will run into problems, follow the error messages to solve the given error.

To know if you have successfully created a Rails project, run the server with rails s command.

rails s

In your terminal you will be in a position to see the following:

=> Booting Puma
=> Rails 7.0.4 application starting in development 
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.6.5 (ruby 3.0.4-p208) ("Birdie's Version")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 66980
* Listening on
* Listening on http://[::1]:3000
Use Ctrl-C to stop

2. Creating Rails web page

To create web pages in Rails, we use Model View Controllers (MVC). The views is what gives the UI,controllers is what determine the pages to be rendered, and models is used to interact with the database. To create a rails webpage we rails generator command.

rails g controller home index

To check what has been created, start rails server and head to your browser and type the following:


You will be greeted with homepage showing Home#index. For me this homepage route is long, what we should do is whenever we hit localhost:3000, it automatically takes me to the homepage. Let me show you how to do that.

Go to config folder and look for routes.rb and add the following and make sure you comment out the default route get 'home/index.

#get 'home/index
root 'home#index

You are now in a position to hit your homepage directly.

You can also check to see your current routes you have created with rails routes command.

rails routes 

3. Creating CRUD functionalities

CRUD means create, read, update and delete. Its one of the main functionalities of any website. You need users to view your products, you need to add items to the database, you sometimes want to update the given product or you sometimes wants to remove a certain item from the database. All these happens with CRUD ability. So lets see how to perform crud in rails.

To enable crud, we use rails g scaffold command. So lets start creating for our project.

rails g scaffold products name:string description: string price:string quantity: string

Once you run the following command, you can see something like this:

invoke  active_record
      create    db/migrate/20221121115223_create_products.rb
      create    app/models/product.rb
      invoke    test_unit
      create      test/models/product_test.rb
      create      test/fixtures/products.yml
      invoke  resource_route
       route    resources :products
      invoke  scaffold_controller
      create    app/controllers/products_controller.rb
      invoke    erb

After this, we need to create a database schema. To achieve this, we need push our migrations to the database with the following command rails db:migrate command

rails db:migrate

Run your server again to see what had been added.

If you head into home/products directory on your browser you will see the following:


So now you are in a position to create new products. Hit new product and you will see a nicely build form.

Nextgentips:Create product
Nextgentips:Create product

From here now you can test all the CRUD functionalities. I hope you enjoy hacking the rails development. Happy coding

About Kipkoech Sang

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Kipkoech Sang →

Leave a Reply

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