Rails meets Sinatra
Published over 5 years ago

For real !

It was pleasantly simple to get Rails + Sinatra run in the same process.

First of all, put your Sinatra application inside RAILS_ROOT. My Sinatra app is called tiny :

# RAILS_ROOT/tiny.rb
require 'sinatra'

before { request.env['PATH_INFO'].gsub!(/\/$/, '') }

get '' do
  'Hello Sinatra!'

And then do the rack dance – racked.rb :

# RAILS_ROOT/racked.rb
require File.dirname(__FILE__) + '/config/environment'
require 'thin'

# Sinatra stuff
require 'tiny'

# Make sinatra play nice
set :env, :production
disable :run, :reload

app = Rack::Builder.new {
  use Rails::Rack::Static

  # Anything urls starting with /tiny will go to Sinatra
  map "/tiny" do
    run Sinatra.application

  # Rest with Rails
  map "/" do
    run ActionController::Dispatcher.new

Rack::Handler::Thin.run app, :Port => 3000, :Host => ""

Line 25 ensures your Sinatara application is used for all the urls starting from /tiny. Any url not starting from /tiny will go to the Rails application.

Now start your server :

$ ruby racked.rb

If you go to, you’ll be greeted with ‘Hello from sinatra!’. And your Rails application will continue to work as expected for everything else. Voila! There you have the fastest ‘hello world’ Ruby framework embedded with Rails :)

Please note that Rails’ built in rack adapter isn’t perfect and you might find a glitch or two. All in good time

UPDATE 1: Prettify Sinatra code and remove monkey patches, thanks to the feedback from Blake Mizerany. Also add a before filter to sinatra app for removing trailing slash.