SQLite CRUD Tutorials in Ruby/Rails: A Step-by-Step Guide
In this tutorial, we will explore how to perform CRUD (Create, Read, Update, Delete) operations with SQLite using Ruby on Rails.
SQLite is a lightweight, file-based relational database management system, and Ruby on Rails is a versatile web application framework. In this tutorial, we will explore how to perform CRUD (Create, Read, Update, Delete) operations with SQLite using Ruby on Rails. By the end of this article, you’ll have a solid understanding of how to interact with an SQLite database from your Ruby on Rails applications.
Prerequisites
- Ruby installed on your machine.
- Ruby on Rails installed on your machine.
- Basic knowledge of Ruby and Ruby on Rails.
Step 1: Create a New Ruby on Rails Application
Let’s start by creating a new Ruby on Rails application. Open your terminal and run the following command:
rails new sqlitecrudapp
This command will create a new Rails application named sqlitecrudapp
. Navigate to the project directory using cd sqlitecrudapp
.
Step 2: Configure SQLite Connection
In your Rails application, SQLite is the default database, so there’s no need to configure a connection. Rails will automatically set up SQLite as the database for your application.
Step 3: Generate a Model and Migration
Now, let’s generate a model and a corresponding migration for our CRUD operations. In your terminal, run the following command:
rails generate model Task title:string description:text
This command generates a Task
model with title
and description
fields.
Next, apply the migration to create the tasks
table in your SQLite database:
rails db:migrate
Step 4: Implement CRUD Operations
Create (C)
In your tasks_controller.rb
file, add the following code to create a new task:
def create
@task = Task.new(task_params)
if @task.save
redirect_to @task, notice: 'Task was successfully created.'
else
render :new
end
end
This code defines a create
action that creates a new task record in the tasks
table.
Read (R)
To retrieve and display task records, add the following code to your index
and show
actions:
def index
@tasks = Task.all
end
def show
@task = Task.find(params[:id])
end
These actions allow you to display a list of tasks and view individual task details.
Update (U)
For updating task records, add the following code to your edit
and update
actions:
def edit
@task = Task.find(params[:id])
end
def update
@task = Task.find(params[:id])
if @task.update(task_params)
redirect_to @task, notice: 'Task was successfully updated.'
else
render :edit
end
end
These actions allow you to edit and update task records.
Delete (D)
To delete task records, add the following code to your destroy
action:
def destroy
@task = Task.find(params[:id])
@task.destroy
redirect_to tasks_path, notice: 'Task was successfully deleted.'
end
This action deletes a task record.
Step 5: Create Views
Create views to display and interact with your tasks. In the app/views/tasks
folder, create the following files:
index.html.erb
: Display a list of tasks.show.html.erb
: Display an individual task.new.html.erb
: Create a new task.edit.html.erb
: Edit an existing task.
Here’s an example index.html.erb
file:
<h1>Tasks</h1>
<ul>
<% @tasks.each do |task| %>
<li>
<%= link_to task.title, task %>
</li>
<% end %>
</ul>
<%= link_to 'New Task', new_task_path %>
Step 6: Run Your Application
Start your Rails server by running:
rails server
Visit http://localhost:3000/tasks
in your web browser to interact with your CRUD application for SQLite.
Conclusion
In this article, we’ve covered how to perform CRUD operations with SQLite using Ruby on Rails. You’ve learned how to create a Rails application, configure the SQLite connection (which is automatic in Rails), generate models, controllers, and views, and implement the CRUD operations for managing task records. SQLite is a lightweight and easy-to-use database, making it a great choice for small to medium-sized web applications. Remember to handle errors gracefully and adapt these examples to suit your specific project requirements.