EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
Ruby
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
Rubychevron-right

Getting Started with MongoDB Atlas and Ruby on Rails

Luce Carter6 min read • Published Dec 11, 2023 • Updated Dec 11, 2023
AtlasRuby
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
In 2022, I had the pleasure of representing MongoDB at Rails Girls London.
The funny thing is, though, I am not a Ruby developer, let alone a Ruby on Rails developer. Thankfully, this was a well organized event with tutorials to follow, and plenty of coaches if any of us got stuck. So as well as chatting to attendees about MongoDB, I had the chance to follow along and learn something new.
At first, I followed along with the Rails Girls App Tutorial and got up and running with a working Ruby on Rails application. But this uses SQLite (I know, booooo), so I decided to take what I had learned, combined with our own documentation for getting started with Rails, and create a Rails app that allows you to use MongoDB Atlas instead!
Application homepage showing list of ideas
I am forever thinking of new ideas for content or side projects. My brain is buzzing constantly and finds inspiration in many places. So I loved the idea from the Rails Girls tutorial of a website that allows you to store an idea, including the name, description, and a picture to show what sparked that idea.
In this article, I will talk you through what I learned along the way creating this, and how you can make the same thing yourself! If you would rather browse code, it is publicly available on GitHub.

Pre-requisites

You will need to have a few tools in place in order to follow through this article:
  • Free MongoDB Atlas Cluster — this will guide you through creating your first cluster and getting your all-important connection string that will be used in this tutorial.

Creating a project

The first step is to create the application. We can take advantage of the rails cli tool that is part of the Rails installation to bootstrap our new application.
We pass the --skip-active-record option because we won’t be using an ActiveRecord adapter to communicate with our MongoDB database. We also pass -–skip-bundle because we want to modify the application’s Gemfile to include the mongoid gem first before installing the bundle.

Adding and configuring Mongoid

Now we have the project created, we want to configure MongoDB. The first step is to add the mongoid gem. Mongoid is the officially supported object document mapper (ODM) for Ruby. An ODM is the document database equivalent of object relational mapper (ORM) which is used to map between models and the relational database.
  1. Inside your Gemfile, add gem mongoid, which defines that we want to use this gem.
  2. From your CLI, run bundle install to pull down the required files to include mongoid in your application.
  3. Once complete, run the command rails g mongoid:config, which will generate the mongoid.yml file inside your application’s config directory. This configuration file defines how to connect to your MongoDB database.
  4. If you haven’t already, get your connection string from Atlas.
  5. Update the development config section inside config/mongoid.yml with the following code, replacing the uri section with your own connection string:
Now we have our connection string added, we will be able to store our ideas documents inside MongoDB Atas.
We then want to run the application to make sure everything is working correctly.
This will start a server at https://localhost:3000, so open this in your browser of choice and make sure that the page loads. Out of the box, you get a Ruby on Rails logo.

Creating our idea endpoint

Ruby on Rails uses the Model-View-Controller architecture. This means that we will have an Idea model that represents the document we want to store, a view to display that idea in the front end of our application, and a controller that allows us to interact with our ideas.
Thankfully, creating all this is really simple. Within the Rails cli, you can run a command that will scaffold out everything we need, including our model with the properties we specify, the view, and the controller.
In the command above, we specify the three properties we want to include in our idea model. This will generate an output that looks like the code below, showing the actions that command has carried out and the files it has created.
As we learned at the start, out of the box, a Rails application will default to the root page. Now we have an idea endpoint, we want that to be the default page that opens when the app is started.
  1. Open routes.rb inside the config folder.
  2. Replace the contents with the following:

Adding the ability to create new ideas

If we ran our application now, you would see one of two things: either a mostly blank page, or any ideas you added in the database outside this application — for example, using the Atlas UI or MongoDB Compass.
But we want the ability to add new ideas. We also can take this opportunity to make the application look a little better.
  1. Open application.html.erb.
  2. Replace the current file with the following code:
  1. Open application.css.
  2. Replace the current file with the following:
  1. Make sure both files you changed are saved and run it. You should see a page similar to below where we now have a navigation bar at the top with a link to add a new idea.
Homepage of application showing a navbar and any existing ideas
I know it doesn’t look very pretty (if you have good design/CSS skills, I’m sure you could do wonderful things with this), but if you click New Idea now, it will load a form that gives you the ability to create a new idea.
You will notice that we never created this form. This is one of the powers of Ruby on Rails. Because we scaffolded out our idea model, it generated what we need to interact with it too.
Clicking the Create Idea button will then cause you to return to the idea details screen with a “success” message and your newly created idea.
New idea created with name and image You can then click to edit the idea further by using the hyperlink, or by clicking back to Ideas to view your new idea in your list of ideas. There is also a button here to delete the idea. We didn’t have to configure any of it — phew!
The best part is, thanks to us configuring the MongoDB driver, this idea will be stored in Atlas and you can see the idea inside your database, using any of the tools available. Below is an image of this being viewed inside Compass.
Screenshot of MongoDB Compass showing the new idea is stored

Summary

Just like that, with a few CLI commands and some code changes, we have a working Ruby on Rails application that allows us to create, read, update, and delete ideas, all stored inside MongoDB Atlas for free!
Of course, as mentioned earlier in the article, this doesn’t look great, so you may want to play with the styling to make it look better. But that is outside the scope of this article.
But one feature we are going to add in a future post you can look out for is the ability to search for an idea using Atlas Search, MongoDB’s full-text search feature that runs on Apache Lucene.
If you have any questions, our community forums are always the best place to start.
Thanks again to Rails Girls London for inspiring this post and for the excellent guide that gave such a great foundation to build on!

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
News & Announcements

A Plan for MongoDB and JRuby


Jun 23, 2022 | 2 min read
Tutorial

Calling the MongoDB Atlas Administration API: How to Do it from Node, Python, and Ruby


Apr 13, 2023 | 4 min read
Article

TableCheck: Empowering Restaurants with Best-in-Class Booking Tools Powered by MongoDB


Dec 14, 2022 | 4 min read
Article

Why Use MongoDB with Ruby


Oct 07, 2022 | 4 min read
Table of Contents