September 7, 2010

Sinatra and Padrino

Filed under: Uncategorized — Tags: , — yasi8h @ 4:16 am

I am doing some R&D on Sinatra and Padrino at work. As a part of that i am going to write this blog post about Sinatra and Padrino. I always wanted to learn about these neat libraries and frameworks in the ruby world. And now i am getting paid to do it ;).

My main focus would be on Padrino. However i would give a basic introduction in to Sinatra (so that i can learn about it in the process). But please keep in mind that i am no expert in Sinatra or Padrino. So if you are just getting started with these you should read the official documentation that can be found on their official sites (Sinatra and Padrino).


Sinatra is basically a DSL for creating web applications easily (you can create a web app that outputs hello world in approx 6 lines!).

# myapp.rb
  require 'rubygems'
  require 'sinatra'
  get '/' do
    'Hello world!'

Sinatra is not a full fledged feature rich web application framework like Ruby on Rails. It is not meant to be anything like that. But its light weight and it suites for certain types of web applications. For example if you want to build  a web app that ‘tries to guess who your real friends on twitter are and who is not’, Sinatra would be a good choice. As this application is a ‘tiny web app’.

Sinatra can also be used to write Rack middleware, Rack Applications and Rails metals (read the Sinatra::Base – Middleware, Libraries, and Modular Apps section in Sinatra: README).

Main Features of Sinatra

These are some of the main features that you’ll get with Sinatra.

  • Routes
    • You can use routes to capture HTTP requests from clients and respond to them. A typical Sinatra application would consists of many such routes. You can use http verbs such as get, put, delete…etc in these.
    • They can include named parameters such as the example (taken from the official readme). The Routes are matched/prioritized according to the order they are defined in source code.
    • get '/hello/:name' do
          # matches "GET /hello/foo" and "GET /hello/bar"
          # params[:name] is 'foo' or 'bar'
          "Hello #{params[:name]}!"

  • Views
    • You can use views with Sinatra. It supports static files (ex: example.html, style.css…etc) and rendering engins like haml. You can put your views in separate files or write them inline.
    • And just like in rails whatever instance variable you set in the context of your routes can be accessed in your views.
  • Helpers
    • You can define and use helpers in your routes and views.
  • Filters
    • You have before and after filters. They would run before a request is sent to the appropriate route and after a response is sent by a route. You can access and modify the request and the response object in before and after filters respectively. You can set instance variables in filters that can be accessed by other filters and routes. And whatever instance variables you set in routes can be accessed in after fileters.
  • Error Handeling
    • Sinatra have error handlers that lets you handel exceptions (things like NotFound a.k.a. 404 or your own custom exceptions.

Sinatra is a grate tool to get something done without having to put up with fat web frameworks like Ruby on Rails. But it might not be a very practical choice if you want to do a complete web application with all the bells and whistles. This is where Padrino comes in. It is build on top of Sinatra and adds a significant amount of features to Sinatra.


When learning about this framework i took most sensible approach a person could take. To follow the official guide and try creating a sample blog! Every web application framework has a tutorial on ‘how to create a blog with framework X in 5 minutes’ these days :).

I managed to follow the tutorial without much problems and create the application with little time. I did ran in to a small problem on the way though. So let me tell you that if you ever try to run something like $ padrino rake ar:seed. There is no such task. You should instead try running $padrino rake seed (this generates some authentication data for your admin application, there by letting you log in to the admin application, in your project. This is of course assuming you generated a admin application).

Comparing with Sinatra, i definitely liked the generators that Padrino provides. I don’t think generators would add a lot of overhead to a application. As long as they don’t generate too much bloat (like rails did sometime back). Padrino will definitely remind you of rails if you have ever used it. But its a bit different.

Padrino is framework agnostic (at least they claim to be). Wich is really nice because that mean you can use any (almost) framework you want with it. It can be very modular this way as you can choose and build your application with all your favorite frameworks. So the generator lets you choose what frameworks you want to use for testing/ORM/templating/jscripting…etc. I Initially used the options used in the guide. Then I tried using a different ORM (I used ActiveRecord initially) which is DataMapper (which was new to me).

Ramaze is a another web framework (there are so many frameworks in the ruby world… i guess its a good thing) that is both agnostic and modular some what similar to Padrino.

The file structure in Padrino is much simpler when comparing with rails. Its easier to work with. But i didn’t like having to write explicit statements to render views inside my routes. In rails this would not be needed (usually). The reason given by the Padrino team for this that its more clean and concise. They do have a point but i would prefere learning about some conventions over having to explicitly write statements to render my views every time i want to render a view.

Padrino supports something called ‘Mountable Apps’. Just like Rails Engines. It basically lets you have a app(s) inside a app. Although i am not exactly sure where (some sample usages are given in the Mountable Application section in i would use this functionality, it sound pretty cool.



Blog at