Manually Resolving a Rails Route

(Rails 3.1)

We had a need to take a string that had a route (/accounts/1) and get back the controller it belonged to (AccountsController).

The way to do that is:


which returns

:action => "show",
:controller => "accounts",
:id => "1"

This appears to be undocumented, but ohh well. Now it’s written up here :)

Ruby Spreadsheet Gem Doesn’t Play Nice With BigDecimal

In a report I was building, we were doing all our underlying math in BigDecimal for accuracy. But when I go to spool it out to an excel export via Spreadsheet, it fails pretty miserably with very odd behaviors.

A cell that should have ’1844.05′ instead had the value: ’1844.046875′.

I didn’t go hunting much further than lib/spreadsheet/excel/writer/worksheet.rb, but there are lots of explicit typechecks in there, and apparently BigDecimal confuses them.

Quick Fix: use .to_f on your BigDecimals before handing them off

Correct Fix: Use the Axlsx gem instead, which seems a bunch more stable, and nicer. Plus it spits out .xlsx files instead of the stupid BIFF8 .xls files.

Sinatra Cookie handling in 0.9.4

I had an older post about cookie handling with Sinatra, and a few details are out of date.  This is a current view (as of September 2009) of how cookies work in Sinatra.

First, you need to set cookies via the response object, as Sinatra no longer has a helper for it.

Second, set_cookie always takes two arguments. If you need to set advanced options, pass a hash:

There are a good handful of options available. One gotcha with the expires option is that it must respond to .gmtime, which the Date object does not. So you need to use Time or DateTime as far as I can tell.

To read the underlying code for setting cookies, it’s in the rack gem, rack-1.0.0/lib/rack/response.rb line 56.

To fetch cookies, it’s simply calling the cookie method:

Monk – Quickstart Sinatra Projects

A few coworkers at Citrusbyte (@soveran, @djanowski) have been working on Monk, a new open source web framework.  At its core is Sinatra, with a set of bundled technologies to help new projects get moving as quick as possible.  It helps you figure out the best structure for your Sinatra app, and gets you moving quickly with logging, settings, and testing. 

Monk consists of several components:

Monk Binary

At the core of Monk is a command line tool for starting new projects.  Think of the rails command, and what it does and you have a good idea of what the monk command does.  It lists, manages and instantiates new skeletons for your project. The Monk binary can instatiate any skeleton you want, which means it is a great way to start projects in any language.

Monk Skeletons

A Monk skeleton is a set of files defining the structure of a new project.  There are almost no rules here, the new project can be Sinatra, Django, an ircbot setup, or anything else you want, heck, it could even be Rails. 

Monk Default Skeleton

The default Monk skeleton is where lots of work has gone in, and where you’ll find the meat of Monk.  It’s a set of amazing tools forming a full-stack Sinatra based web development platform.

  • Sinatra – super lightweight web DSL. You know what this is (and if you don’t, go read the rest of this blog, and check it out)
  • Ohm – data access layer using Redis, a stupid fast key-value database.  Super simple models for your app
  • Monk Glue – a Reloader, Logger, and Settings handler for your new application. Don’t worry about it, Sinatra Logging finally made easy…  Monk Glue is destined to keep growing too, including a handful of mix & match helpers for common tasks.
  • Rack-Test, Contest, Stories, Webrat, Faker, Spawn – An amazing unit and integration test stack, allowing both low level unit tests of your code, and high level integration tests, similar in style to Cucumber, but without that nasty repeating-yourself part.  You owe it to yourself to try the stories & webrat approach to testing your Sinatra application.
  • Dependencies – trivial management of all the gems you rely on. Just define a dependencies file, and the dependencies gem will handle vendoring and unpacking your requirements.

Monk Glue

Logging – a perennial problem and hassle in setting up new projects is now simple with Monk, and Monk Glue. 

Reloader – an alternative to the classic Sinatra reloading (with weird require related issues), and Shotgun, which causes a fork() each request, which is fairly heavyweight.  Glue’s Reloader is the happy midpoint, between the other two solutions. It reloads everything, but in the same Ruby process, making it much faster.

Settings – a minimalist implementation of the settings.yml file, with environment support. Easily switch up your database settings, your email address, url and more.  Bonus Feature: the default skeleton creates an automatic settings.yml file when you first run monk:start. 


Ohm Persistence Layer

Ohm is a lightweight data management layer that uses Redis as it’s backend. Redis is a fast key-value database, and Ohm makes mappings that are simple to define, and automates the nasty parts of key-value databases like reverse lookups (indexes). Ohm isn’t technically an ORM, since there’s no relational database backing it, but it fills the same niche as ActiveRecord or Datamapper, storing your data for future use.

Awesome Reddit Clone Example

Check out, the code is located at

Pay attention for a future blog post digging further into this example code, and dissecting it.

Get Monk & Find out More

Install Monk


Wrapping get, post, put, delete – Add Magic JSON Power

Several times recently, there have been questions in the #sinatra channel, and on the mailing list about how to automatically render JSON, or any other kind of format as the response from Sinatra.

My favorite way of accomplishing this is to wrap the normal get and post methods with additional JSON abilities.

And of course, this can be expanded to other cases where you’d want to pre-process or post-process certain routes. You could easily define an admin_get that checks that the user is logged in as an admin. Or an xml_get, or a timed_get method.

Rendering different layouts with Sinatra

I had a project recently that had a requirement of a frontend, and an admin backend. Pretty normal for an application, but of course the admin had a different layout, set of tabs, etc. I didn’t want to say :layout => ‘admin’ everywhere, so I came up with this (very small) code snippet to make rendering a different layout easy.

Which I then used in my code like:

This also shows how to have nested directories in the views folder if you were curious. This will look for the view views/admin/users/index.haml, and render it with the layout views/admin/layout.haml.

Installing Sinatra 0.10.1

The current version of Sinatra (as of the time I write this) is version 0.10.1. The gem on rubyforge is outdated (0.9.2). To install the current gem, use the github built version:

ActiveRecord timezones without Rails

I was using Active Record in a batch application to manipulate some data, and I needed the timezone handling built into the newer versions. But I had a hell of a time trying to figure out how to make it all work.

In plain Rails, it’s simple:

But I wasn’t running Rails proper, and didn’t want to pull in the whole Rails boot sequence. So the Rails::Initializer call didn’t work. To do it manually, time zone config turns out it’s a 3 lines of setup calls:

When you do that, time zone attributes work both going into the database, and coming back out. If you miss the last line, they’ll just not work coming out, which is a damn confusing thing to deal with.

Ohh, and one other thing I ran into when getting this setup is that AR pulls in Active Support, which in turn pulls in Builder (to hack xml support or something). Just watch out for that, you’ll get weird crashes if you don’t have builder installed. Honestly, I just went and commented out the areas of Active Support that did it, I didn’t want builder anyway.

Fix for: “no such file to load — blankslate”

I was getting this error, and it turns out it’s just that builder isn’t installed. sudo gem install builder, and you’re fixed.