MongoDB 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 MongoDB using Ruby on Rails.
MongoDB is a popular NoSQL database that can be easily integrated with Ruby on Rails, a powerful web application framework. In this tutorial, we will explore how to perform CRUD (Create, Read, Update, Delete) operations with MongoDB using Ruby on Rails. By the end of this article, you’ll have a solid understanding of how to interact with a MongoDB database from your Ruby on Rails applications.
Prerequisites
- Ruby installed on your machine.
- Ruby on Rails installed on your machine.
- A running MongoDB server.
- 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 mongocrudapp
This command will create a new Rails application named mongocrudapp
. Navigate to the project directory using cd mongocrudapp
.
Step 2: Configure MongoDB Connection
In your Rails application, you need to configure the connection to MongoDB. Open the config/mongoid.yml
file and make sure it looks like this:
development:
clients:
default:
database: your-database-name
hosts:
- your-mongodb-host:27017 # Replace with your MongoDB server host and port
options:
user: your-username
password: your-password
auth_source: admin
Replace 'your-database-name'
, 'your-mongodb-host'
, 'your-username'
, 'your-password'
with your MongoDB server details.
Step 3: Generate a Model
Now, let’s generate a model for our CRUD operations. In your terminal, run the following command:
rails generate model Article title:string content:text
This command generates an Article
model with title
and content
fields.
Step 4: Implement CRUD Operations
Create (C)
In your articles_controller.rb
file, add the following code to create a new article:
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article, notice: 'Article was successfully created.'
else
render :new
end
end
This code defines a create
action that creates a new article document in MongoDB.
Read (R)
To retrieve and display article records, add the following code to your index
and show
actions:
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
These actions allow you to display a list of articles and view individual articles.
Update (U)
For updating article records, add the following code to your edit
and update
actions:
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article, notice: 'Article was successfully updated.'
else
render :edit
end
end
These actions allow you to edit and update article documents.
Delete (D)
To delete article records, add the following code to your destroy
action:
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path, notice: 'Article was successfully deleted.'
end
This action deletes an article document.
Step 5: Create Views
Create views to display and interact with your articles. In the app/views/articles
folder, create the following files:
index.html.erb
: Display a list of articles.show.html.erb
: Display an individual article.new.html.erb
: Create a new article.edit.html.erb
: Edit an existing article.
Here’s an example index.html.erb
file:
<h1>Articles</h1>
<ul>
<% @articles.each do |article| %>
<li>
<%= link_to article.title, article %>
</li>
<% end %>
</ul>
<%= link_to 'New Article', new_article_path %>
Step 6: Run Your Application
Start your Rails server by running:
rails server
Visit http://localhost:3000/articles
in your web browser to interact with your CRUD application for MongoDB.
Conclusion
In this article, we’ve covered how to perform CRUD operations with MongoDB using Ruby on Rails. You’ve learned how to create a Rails application, configure the MongoDB connection, generate models, controllers, and views, and implement the CRUD operations for managing article documents. MongoDB is a flexible and scalable NoSQL database, and integrating it with Ruby on Rails allows you to build dynamic web applications. Remember to handle errors gracefully and adapt these examples to suit your specific project requirements.