Rails UI and Sinatra API in One App

Noah Gibbs

Abstract

Rails lets you embed any other Rack apps right in your routes.rb. You'll see how to add a Sinatra mini-API and a raw Rack app. We'll also build a raw Rack app. We'll use simple authentication across all apps as an example of Rack middleware. I'll show you how to share ActiveRecord models between Rails and Sinatra. The same tricks work with basically all Ruby HTTP frameworks, such as Cuba, Padrino and of course plain Rack.

Tags: Intermediate

Details

This is intended for beginner to intermediate Rails developers. They've probably heard of Rack but haven't done much with it directly. They may know how to use middleware.

After the session, they should be able to embed a Sinatra (or other Rack-based) app inside their Rails app, including one that shares underlying models, libraries or gems. They'll also be able to build a simple Rack app and add Rack middleware in places where Rails encourages that.

The session will use a simple "demo" Rails and Sinatra app that uses Rails for a UI and Sinatra for an API server, as an example of how/why you'd combine them. It's intended as a way of addressing criticisms of Rails by showing how Rails combines with other frameworks: "Rails isn't good at X, but Y microframework is. Don't use Rails" becomes "use both."

This method of decomposition can also be used to separate a larger Rails app into multiple smaller apps, even if the smaller apps use another framework. The session's code shows them all in one process, though that's not an absolute requirement. But the single-process approach lets you combine Rack middleware and ActiveRecord models across all the apps, which is a big non-obvious benefit.

I considered submitting this as a workshop. Any opinion on whether that would be better? There isn't that much code code, though it does require a small Rails app and a small Sinatra app to use together.

Pitch

Rack middleware is incredibly powerful. Rails is built from the ground up to use Rack. Rails itself is written as many layers of middleware, making Rack even more powerful in Rails. Rack is also surprisingly poorly known and poorly understood. There's a lot of great Rack-based software out there that's easy to leverage, but most people don't even know how to start. One great starting point: adding in Rack apps and middleware to a Rails app.

I give various Rack talks pretty often (meetups, talks for companies I work for, a couple of conferences.) I have some popular-ish blog posts on deeper Rails and Rack details. I've been the Ruby/Rails guy at my last three companies, and the "go to" person for coworkers learning/improving their Ruby and Rails skills. I also did an intensive thing training people in Rack that I don't know how to talk about without identifying myself (it's in my speaker bio.) I'm pretty good at talking about this topic.

Speaker Bio

Noah is a Ruby Fellow for AppFolio, working on the core Ruby language and related tooling. As a father and trained hypnotherapist, Noah uses his powers of evil for good. He wrote the book "Rebuilding Rails," which is universally worshipped as "pretty good."

Edit proposal

Submissions

RailsConf 2017 - Rejected [Edit]

Add submission