Getting Started with MongoDB Atlas and Ruby on Rails
Rate this tutorial
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.
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.
You will need to have a few tools in place in order to follow through this article:
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-recordoption because we won’t be using an ActiveRecord adapter to communicate with our MongoDB database. We also pass
-–skip-bundlebecause we want to modify the application’s
Gemfileto include the
mongoidgem first before installing the bundle.
Now we have the project created, we want to configure MongoDB. The first step is to add the mongoid gem. 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.
- Inside your Gemfile, add
gem mongoid, which defines that we want to use this gem.
- From your CLI, run ```bundle install`` to pull down the required files to include mongoid in your application.
- 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.
- Update the development config section inside
config/mongoid.ymlwith 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.
Ruby on Rails uses the Model-View-Controller architecture. This means that we will have an
Ideamodel 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.
- Open routes.rb inside the config folder.
- Replace the contents with the following:
But we want the ability to add new ideas. We also can take this opportunity to make the application look a little better.
- Open application.html.erb.
- Replace the current file with the following code:
- Open application.css.
- Replace the current file with the following:
- 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.
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.
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.
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.
Thanks again to Rails Girls London for inspiring this post and for the excellent guide that gave such a great foundation to build on!
Calling the MongoDB Atlas Administration API: How to Do it from Node, Python, and Ruby
Apr 13, 2023