How to make a Ruby Web Application, using Sinatra with the Help of Swoonatra

June 14, 2013

In an effort to get acquainted with Ruby in relation to making web applications, I ended up at a fork in the road. Ruby on Rails or Sinatra, which vehicle to web application nirvana do I choose? Letting my DevOps bias take over I ended up choosing what appears to be a more lighter weight yet powerful DSL in Sinatra.

My goal was to get a feel for setting up a basic web app self contained without thinking to hard about scale just yet. I will cover scaling Sinatra in subsequent blogs. Basically get my feet wet in the world of Ruby MVC and DSL’s.

After much reading, messing around and testing the waters with Sinatra I decided to combine some of the basic things a web app might need and make a template/scaffold of code called Swoonatra which I open sourced on Github. The idea is that through the functional examples given in Swoonatra you can get started quicker without the extra research time I had encountered when using Sinatra.

Step 1 Install Your Basic Prerequisites:

I was working off a CentOS 6 Server depending on what is installed on your server you may need to add the following as superuser:

yum install gcc.x86_64
yum install gcc-c++.x86_64
yum install make
yum install openssl.x86_64
yum install openssl-devel.x86_64
yum install openssl098e.x86_64
yum install diffutils.x86_64
yum install gperf
yum install git
yum install sendmail

Step 2 Install Your Ruby Language and MySQL Server:

gunzip -c yaml-0.1.4.tar.gz | tar -xvf -
cd yaml-0.1.4
./configure --prefix=/usr/local
make install
cd ../
gunzip -c ruby-1.9.3-p429.tar.gz | tar -xvf -
cd ruby-1.9.3-p429
./configure --prefix=/usr/local --enable-shared --disable-install-doc --with-opt-dir=/usr/local/lib
make install
yum install mysql-server.x86_64
yum install mysql-server-devel.x86_64
yum install mysql.x86_64
mysql -u root -p
CREATE USER 'yourusernamehere'@'localhost' IDENTIFIED BY 'yourpasswordhere';
GRANT ALL PRIVILEGES ON * . * TO 'yourusernamehere'@'localhost';
create database yourdatabasename;

Step 3 Install Some Ruby Gems:

gem install sinatra
gem install unicorn
gem install dm-mysql-adapter
gem install datamapper
gem install bcrypt-ruby
gem install pony
gem install sinatra-flash

Step 4 Clone Swoonatra from Github:

(Do this in the location where you would like your project to be)

git clone

Now to get up and running you just have to make a few changes to Swoonatra making it custom to your environment

1. In model/model.rb change the dbname/dbpassword/dbname to a mysql server db you have created with a user/password that has access.

DataMapper.setup(:default, 'mysql://dbusername:dbpassword@localhost/dbnamehere')

2. In routes/user.rb change @reset_path to the hostname of your server as well as the Pony Gem Email settings.

if user = Users.first(:email => (params[:email]))
@reset_path = "http://yourhostname/reset?email=#{params[:email]}"

In two places change the following

  :from => '',

3. In routes/contact.rb change Pony Gem Email settings.

  :from => '',
  :to => '',

Start Your Engines!

Go into the project directory and start the unicorn HTTP server up! Then go to your hostname in a browser.

unicorn -c unicorn.conf -D

One extra note make sure you stop and start unicorn when making changes things other then views to see your changes take effect.

killall unicorn
unicorn -c unicorn.conf -D

Why Swoonatra?

Swoonatra was made to save time in learning Sinatra by seeing some examples and having a template to work from. This is not a replacement for reading the Sinatra or other gem documentation but to help provide examples and a structure. Some good Sinatra books to reference are: Sinatra: Up and Running and Jump Start Sinatra

The code covers usages of DataMapper an Object Relational Mapper written in Ruby using MySQL with a foreign key relationship. Also user signup, sign-in, password reset, session handling. For design the code shows usage of URL routing, views, partial views, layouts, error handling, flash messaging. Also included is samples of emailing data, and there is a basic Unicorn web server configuration. Everything is laid out with a MVC structure. Hope this helps!

Comments are closed.