Create a Ruby on Rails Application with Simple CRUD Functionality.
Course Title: Ruby Programming: From Basics to Advanced Techniques
Section Title: Introduction to Ruby on Rails
Topic: Build a basic Ruby on Rails application with simple CRUD functionality. (Lab topic)
Overview
In this lab exercise, we will build a basic Ruby on Rails application with simple CRUD (Create, Read, Update, Delete) functionality. This exercise is designed to give you hands-on experience with Rails and help you understand how to create a fully functional web application.
Prerequisites
Before starting this lab, make sure you have:
- Installed Ruby and Rails on your system (refer to the Rails installation guide if you haven't already).
- Set up aRails development environment.
- Familiarity with the basics of Rails (MVC architecture, routing, etc.).
Step 1: Create a new Rails application
Open a terminal and run the following command to create a new Rails application:
rails new crud_app
This will create a new Rails application called crud_app
.
Step 2: Configure the database
In this lab, we will use SQLite as our database. Open the config/database.yml
file and make sure it looks like this:
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
Step 3: Create a model
In Rails, a model represents a database table. Let's create a Book
model:
rails generate model Book title:string author:string
This will create a book.rb
file in the app/models
directory and a books
table in the database.
Step 4: Run the migration
To create the books
table in the database, run the following command:
rails db:migrate
Step 5: Create a controller
A controller handles HTTP requests and sends responses. Let's create a BooksController
:
rails generate controller Books
This will create a books_controller.rb
file in the app/controllers
directory.
Step 6: Define CRUD actions
In the books_controller.rb
file, add the following methods:
class BooksController < ApplicationController
def index
@books = Book.all
end
def show
@book = Book.find(params[:id])
end
def new
@book = Book.new
end
def create
@book = Book.new(book_params)
if @book.save
redirect_to books_path
else
render :new
end
end
def edit
@book = Book.find(params[:id])
end
def update
@book = Book.find(params[:id])
if @book.update(book_params)
redirect_to books_path
else
render :edit
end
end
def destroy
@book = Book.find(params[:id])
@book.destroy
redirect_to books_path
end
private
def book_params
params.require(:book).permit(:title, :author)
end
end
These methods handle the CRUD operations:
index
: lists all booksshow
: shows a single booknew
: creates a new bookcreate
: saves a new bookedit
: edits an existing bookupdate
: updates an existing bookdestroy
: deletes a book
Step 7: Add routes
In the config/routes.rb
file, add the following routes:
Rails.application.routes.draw do
resources :books
end
This will create the following routes:
GET /books
: lists all booksGET /books/:id
: shows a single bookGET /books/new
: creates a new bookPOST /books
: saves a new bookGET /books/:id/edit
: edits an existing bookPATCH /books/:id
: updates an existing bookDELETE /books/:id
: deletes a book
Step 8: Create views
Create the following views:
index.html.erb
: lists all booksshow.html.erb
: shows a single booknew.html.erb
: creates a new bookedit.html.erb
: edits an existing book
You can use the following code as a starting point:
<!-- app/views/books/index.html.erb -->
<h1>Books</h1>
<ul>
<% @books.each do |book| %>
<li>
<%= link_to book.title, book_path(book) %>
</li>
<% end %>
</ul>
<!-- app/views/books/show.html.erb -->
<h1><%= @book.title %></h1>
<p>Author: <%= @book.author %></p>
<!-- app/views/books/new.html.erb -->
<h1>New Book</h1>
<%= form_for(@book) do |form| %>
<%= form.label :title %>
<%= form.text_field :title %>
<%= form.label :author %>
<%= form.text_field :author %>
<%= form.submit %>
<% end %>
<!-- app/views/books/edit.html.erb -->
<h1>Edit Book</h1>
<%= form_for(@book) do |form| %>
<%= form.label :title %>
<%= form.text_field :title %>
<%= form.label :author %>
<%= form.text_field :author %>
<%= form.submit %>
<% end %>
Conclusion
In this lab, we created a basic Ruby on Rails application with simple CRUD functionality. We defined a Book
model, created a BooksController
with CRUD actions, added routes, and created views to display and edit books.
What's next?
In the next topic, we will explore the routing system in Rails and learn how to create custom routes and actions.
Questions or comments?
If you have any questions or comments about this lab, please leave them below.
External resources:
Images

Comments