Setting up a Ruby(Sinatra) and MongoDB Based App Server

  • Install RVM and Ruby
\curl -sSL | bash -s stable --ruby
  • Next, install bundler and Sinatra using RubyGems
gem install bundler
gem install sinatra
  • Create a file named Gemfile with the following contents in your project directory. You can also use and IDE for creating a new project.
source ‘'gem ‘sinatra’, ‘~> 2.0’
  • Run the following commands to install the dependencies:
bundle install
  • To install mongo in Ubuntu type the following commands:
sudo apt-key adv — keyserver hkp:// — recv 7F0CEB10echo ‘deb dist 10gen’ | sudo tee /etc/apt/sources.list.d/mongodb.listsudo apt-get updatesudo apt-get install -y mongodb-orgTo install in Sinatra, go to the Gemfile that you created earlier and add the following line:gem 'mongoid', '~> 7.0'
  • Run bundle install again to install the dependencies.
sudo service mongod start
database: blog_development
- localhost:27017
server_selection_timeout: 1
  • Create a file named app.rb with the following contents:
require 'sinatra'
require 'mongoid'
  • Load the Mongoid configuration file and configure Mongoid. This is done automatically when Mongoid is used with Rails, but since we are using Mongoid with Sinatra, we need to do this ourselves:
Mongoid.load!(File.join(File.dirname(__FILE__), 'config', 'mongoid.yml'))
class Post
include Mongoid::Document

field :title, type: String
field :body, type: String

has_many :comments

class Comment
include Mongoid::Document

field :name, type: String
field :message, type: String

belongs_to :post
  • Read all the posts
get '/posts' do
  • Create a new post
post '/posts' do
post = Post.create!(params[:post])
  • Read a single post in detail with all the comments
get '/posts/:post_id' do |post_id|
post = Post.find(post_id)
comments: post.comments,
  • Create a new comment on a post
post '/posts/:post_id/comments' do |post_id|
post = Post.find(post_id)
comment = post.comments.create!(params[:comment])
bundle exec ruby app.rb
curl http://localhost:3000/posts
# => []

curl -d 'post[title]=hello&post[body]=hello+world' http://localhost:3000/posts
# => {"_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"},"body":"hello world","title":"hello"}

curl http://localhost:3000/posts
# => [{"_id":{"_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"},"body":"hello world","title":"hello"}]

curl -d 'comment[name]=David&comment[message]=I+like' http://localhost:3000/posts/5d8151ec96fb4f0ed5a7a03f/comments
# => {}

curl http://localhost:3000/posts/5d8151ec96fb4f0ed5a7a03f
# => {"_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"},"title":"hello","body":"hello world","comments":[{"_id":{"$oid":"5d8157ac96fb4f20c5e45c4d"},"message":"I like","name":"David","post_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"}}




Founding Member at Zomentum

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

GSoC 2015 — Moorsp plugin for Moodle — Wrap Up

How to Set-Up Virtual Environment for Python

Rule Engine — Drools ( How to configure Stateful session )

Linux Networking Commands

2. Serverless Framework [CZ]

8 Tips For Using Python Logging

Start using Vim today

Unity’s Animator Controller as a State Machine

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nagesh Bansal

Nagesh Bansal

Founding Member at Zomentum

More from Medium

Adding correlation-id to Node.js express-based application with log4js

Exponential backoff algorithm in NodeJS

NodeJS — how to close your MySQL connections and why

How Auto Code Generator Works