Render ‘Rails Style’ Partials in Sinatra

We love Sinatra. Not only does it make a great framework in its own right but in addition it can be used to mimic parts of rails in a real simple environment for front-end designers. Instead of having to get them set up and explain the whole of rails they just get a nice simple app to work on without having to worry about creating different controllers or even models.

Although there is not a 1 to 1 translation between a rails app and a sinatra one, it does allow these developers to work with things like haml in a really easy to work with environment.

One of the features that I was asked for recently though was “How do you render a partial in sinatra?”

Rendering Partials in Sinatra

Sinatra is a super-lightweight framework. Because of this it doesn’t have the notion of partials built into it. However, a partial, in its simplest form, is nothing more than a call out to render the template as a string and then embed that string into your page.

A quick look at the sinatra sites FAQs shows that partials can be rendered in the following way in erb.

<%= erb(:mypartial, :layout => false) %>

In haml you could use exactly the same thing but call haml like so.

= haml(:mypartial, :layout => false)

Notice that

:layout => false

is set to ensure that the layout is not also rendered.

Going a little further

The FAQs also recommend using the code in the following gist.

The code shows a helper method called partial. This helper method can be used to render a partial from your code. The helper also allows you to pass collections and is a really cool and useful piece of code.

Making things work the rails way

The above helpers are great and really useful for sinatra. However, what if you want to render a partial the ‘rails way’? In our situation we were using sinatra as a mock up of what would eventually be brought into a rails app. Rails allows partials to be included like so:

<%= render :partial => 'partial_name' %>

By overriding the built in render method in Sinatra it is actually possible to mimic the rails partials. I came up with the following helper to quickly mock things up. The helper checks to see if the first argument passed to is a hash and if that contains they key :partial. If so it renders the partial, if not it just uses the default render method.

  helpers do
    def render(*args)
      if args.first.is_a?(Hash) && args.first.keys.include?(:partial)
        return haml "_#{args.first[:partial]}".to_sym, :layout => false

The helper could easily by extended to allow for collections etc but for now it does the job. Any better solutions?

Rounded Corners with an arrow on a UIView with the iPhone SDK 3.0

This article is a follow-up to a previous article Rounded Corners on a UIView with the iPhone SDK 3.0.

In the previous post I included the code to create a UIView with rounded corners the easy way using the iPhone SDK 3.0. However the next challenge came when I wanted to also add an arrow to the edge of the rounded box.

In order to add the arrow I would be manually creating the path to draw the new type of box. I added a variables:

  • (CGFloat) pointY to specify where on the left hand edge the point starts.
  • (CGFloat) pointWidth to specify the width of the point.
  • (CGFloat) pointHeight for the height.
  • (UIColor) rectColor to set the color of the new rectangle (I actually copy the value of self.backgroundColor and set the background color to [UIColor clearColor]

I added these variables so they can be set on the view but I will leave these for you to implement however you like.

Once you have created the variables pointY, pointWidth, pointHeight and rectColor you can then use the following code.

- (void)drawRect:(CGRect)rect {
  // Drawing code

  CGContextRef context = UIGraphicsGetCurrentContext();
  CGFloat radius = self.layer.cornerRadius;

  // Make sure corner radius isn't larger than half the shorter side
  if (radius &gt; self.bounds.size.width/2.0) radius = self.bounds.size.width/2.0;
  if (radius &gt; self.bounds.size.height/2.0) radius = self.bounds.size.height/2.0;

  CGFloat minx = CGRectGetMinX(self.bounds) + self.pointWidth;
  CGFloat midx = CGRectGetMidX(self.bounds);
  CGFloat maxx = CGRectGetMaxX(self.bounds);
  CGFloat miny = CGRectGetMinY(self.bounds);
  CGFloat midy = CGRectGetMidY(self.bounds);
  CGFloat maxy = CGRectGetMaxY(self.bounds);

  CGContextMoveToPoint(context, minx, midy);
  CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
  CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
  CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
  CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);

  CGContextMoveToPoint(context, minx, miny + pointY);
  CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
  CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
  CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
  CGContextAddArcToPoint(context, minx, maxy, minx, miny + pointY + pointHeight, radius);
  CGContextAddLineToPoint (context, minx, miny + pointY + pointHeight);
  CGContextAddLineToPoint (context, minx - pointWidth, miny + pointY + (pointHeight / 2));


  [self.rectColor setFill];
  CGContextDrawPath(context, kCGPathFill);

Rounded Corners on a UIView with the iPhone SDK 3.0

An iPhone app with programmatically set cornerRadiusI’ve been doing a bit more iPhone development recently and one of the challenges we encountered was to control the background color of a UIView programmatically. Now this is a pretty simple thing to do until you decide you also want to round the corners of that view.

One option is of course to use an image as a background but this wouldn’t allow us to programmatically change the background color of the UIView.

After a bit of digging I finally came across the method cornerRadius delcared as the following:

@property CGFloat cornerRadius

The corner radius is a property of the layer which is a CALayer. In order to use this though you must be using iPhone SDK Version 3.0 or above and you must include QuartzCore/QuartzCore.h

Another tip was that the corner radius does not work until you set masksToBounds to true. With all of this combined the following snippet should allow you to create rounded rectangles on your UIViews.

#import "QuartzCore/QuartzCore.h"

//To set the rounded corners
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 5.0;

Using Rack::Rewrite to remove the www

Rack::Rewrite is a pretty useful tool. It allows you to change the location of a url or perform 301 or 302 redirects using rack. The real benefit of this is that the logic of your routes is contained within your app and server agnostic. Sure there is a performance hit compared to using mod-rewrite etc. in Apache, Nginx and so on but we feel its worth it in most cases. If you are using caching systems like varnish or squid the redirect can be cached too.

Using Rack::Rewrite to go NoWWW

There has been a bit of a movement for a while on the web to deprecate www. in front of domain names called no-www. As the site says the title www isn’t really of any use anymore. As well as the no-www movement having a single host can also be useful for things like analytics and cleaning up search results.  There are a couple of rails solutions to doing this including a peice of rack middleware at this site

If you are already using Rack::Rewrite though you may as well just use that to make the change. The following is a rule to make the no-www change using rack middleware.

r301 /.*/, {|path, rack_env| "http://#{rack_env['SERVER_NAME'].gsub(/www\./i, '') }#{path}" },
    :if => {|rack_env| rack_env['SERVER_NAME'] =~ /www\./i}

ID free pretty permalink based URL’s in Rails

Slug based URLS

I love the way that rails allows you to make use of REST and create meaningful urls for your web applications. The only problem is that, although these urls are meaningful, having to use a model’s id in the URL can be pretty ugly.

Replacing the model’s id with a meaningful title can be useful to both users and for SEO purposes. A lot of approaches in rails make use of the fact that calling to_i on a string will start at the begining of a string and work forward until there are no numbers like so:

"1".to_i # => 1
"123four".to_i => 123

This has led to a number of rails plugins that allow you to make id and permalink based urls like the following:

acts_as_friendly_param by Chris Farms is one of the plugins that you can use to achieve this.

Rails Slugs Are Bad

Slugs are bad kids. Okay? (There’s actually nothing wrong with slugs that contain an ID and while I don’t have any objection to creating URL’s like the above for some things they just seem unnecessary).

There is however another approach that allows you to use a unique reference for each record in your models. If you store a unique permalink for each model then you can use that link in your models finder in order to find that record like so:


The only problem with this approach is you have to edit all of your code to use the different finder method. Enter the slugs_are_bad plugin. I created the plugin to override rails’ default finder methods. The plugin is based on a couple of other plugins acts_as_friendly_param by Chris Farms and permalink_fu by Technoweenie but was adapted to suit my needs a little more.

Using the plugin

The slugs_are_bad plugin forms a drop-in replacement to create pretty, id-free, urls. In most cases you don’t have to make any changes from the default rails scaffold apart from adding the slugs_are_bad plugin.

  slugs_are_bad(:permalink_attribute, :generate_from)

  # ie. slugs_are_bad(:permalink, :title)
  # will automatically generate a slug-less permalink from the title attribute and store it in the model.

Then in your view
  link_to 'User', => 'foo bar') # nothing new needed here
  # Generates /users/foo-bar instead of /users/1

  # create the user as usual
  User.create!(:name => 'foo bar')

  # To find the model with its nothing else is required
  User.find(params[:id]) # nothing needed here (where id will be 'foo-bar')

  # you could also manually specify the permalink in this instance if wanted.
  User.create!(:name => 'foo-bar', :permalink => 'foo')

However, there are still a few things missing. The plugin is not quite as flexible as I would like it to be and also there are no tests for it at the moment. I have a set of tests that I use in a production site to ensure the plugin works for that project but I havent created any tests for the standalone plugin itself yet. I should also note that the plugin was been created for rails 2.3.x, I know that rails 2.3 is obsolete now :P but hey some people might find it useful.

If anyone wants to expand upon the plugin then fork away on github and ping me the changes.

Using the new Rails 3.0 Gem bundler with Passenger, Mongrel and Heroku

UPDATE 19th Feb 2010: Bundler is moving pretty fast! For the most up to date information I’d checkout and specifically this gist ( to find out the latest!

Recently we were unsure whether we would be deploying a site to our own hosted system or heroku. I love heroku but there are times when it just doesnt suit the project and a bit more fine grained control is nessesary.

In order to use heroku it was suggested that you move over to the new gem bundler that Yahuda has been working on as part of rails 3.0. However it seems there are a couple of different ways to get the bundler running. The default recomended way works great for mongrel and heroku but didn’t play so nice with passenger.

The default is to place all of the following code into config/preinitializer.rb:

require "#{File.dirname(__FILE__)}/../vendor/bundler_gems/environment"

class Rails::Boot
  def run

  def extend_environment
    Rails::Initializer.class_eval do
      old_load = instance_method(:load_environment)
      define_method(:load_environment) do
        Bundler.require_env RAILS_ENV

After a bit of searching around it seems that in some spawn methods this does not work great with passenger.

The solution

Instead use the following:

In your config/preinitializer.rb just include this part

require "#{File.dirname(__FILE__)}/../vendor/bundler_gems/environment"

Then in config/boot.rb place this just before the last Rails.boot! line like so:

# for bundler
class Rails::Boot
  def run

  def extend_environment
    Rails::Initializer.class_eval do
      old_load = instance_method(:load_environment)
      define_method(:load_environment) do
        Bundler.require_env RAILS_ENV

# All that for this:

That should allow you to easily boot from either server and works great with heroku.

Thanks to Mathew Todd for giving the solution based upon gem cutters commit here.

Random, Previous and Next entries from Active Record models using offset

I have seen a couple of entries floating around on the internet relating to how to obtain a random active record entry or the next or previous active record entries. Many of the examples are actually quite inefficient and although they’ll get the job done they’re probably not best for the poor server. I’m certainly not an expert so any corrections on my methods are more than welcome. Offset is often underused feature by rails developers, in my experience, so I thought I’d post a couple of useful use cases for offset.

Obtaining a random record

One of the ways that seems to appear sometimes is to make use of the MySQL database and some custom ordering to return a list of records in a random order and then just limit the result to the first. This would be done using something like the following.

Entry.find(:all, :limit => 1, :order => "RAND()")

This is not efficient in MySQL. Done this would cause your table to take every record and then generate a random number for it. The time that this takes would therefore be proportional to the number of rows in your table! You also wouldn’t really want to say Entry.find(:all).rand because the entire record set would be returned before a random entry is chosen.

Although it might seem like it would take a little longer as there are two calls my preferred solution would be to add the following method to you model (entry.rb)

  def self.random(options = {})
    count = self.count()
    self.find(:first, options.merge(:offset => rand(count)))

This code will find the count of all of the rows in the table and then use this to choose a record with a random offset. The offset is used to make sure this works when records are deleted. While this could also probably be done in the database with the offset being a select statement any named scopes would not be present on the embedded select and you might be choosing a random number from a larger set than is actually available to the query introducing a bias in the random number chosen. Clearly making two calls carries a tiny amount of risk if the number of items changes though but in the worst case you will simply most likely up returning the most recent entry and this method works across any database as there is nothing specific in here.

Getting the next and previous entries

The use of offset can also be applied to get the next and previous records from the database too. I find using a named scope to act as a class method on the model works great.

named_scope :previous, lambda { |i| {:conditions => ["#{self.table_name}.id  "#{self.table_name}.id DESC"} }
      named_scope :next, lambda { |i| {:conditions => ["#{self.table_name}.id > ?",], :order => "#{self.table_name}.id ASC"} }

This specifies the table name of the model automatically to avoid any conflicts that might occur. I even wrapped this one into a tiny little plugin that can be used to save typing this out for different models. The plugin can be found on github


This should have shown a few pretty cool uses for offset but I’d also be interested in hearing responses to the approach for obtaining random records. Does anyone do things differently and how is that method better?


if you need a library to help you pull out more than one random entry then it is worth taking a look at The plugin is pretty lightweight but should help make things a little easier.

Ruby Gems and OSX 10.6 (Snow Leopard)

I recently upgraded to Snow Leopard. I had to fight the urge to install sooner because we had some big projects underway at work and couldn’t afford any downtime.

Due to not having time to do a complete re-install I simply did the upgrade and I must admit I am pretty impressed with how smoothly it went. When I get some more time I’ll do a clean install but I’m happy with things for now.

One of the things I am finding however is that a lot of gems that required native extensions need to be rebuilt. For a number of these things are quite obvious and its just a case of either updating or removing the gem and re-installing.

Autotest however wasn’t quite so obvious. Some gems install dependancies that required rebuilding and its not always 100% obvious like the following:

/Library/Ruby/Gems/1.8/gems/sys-uname-0.8.3/lib/sys/uname.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/sys-uname-0.8.3/lib/sys/uname.bundle, 9): no suitable image found.  Did find: (LoadError)
/Library/Ruby/Gems/1.8/gems/sys-uname-0.8.3/lib/sys/uname.bundle: no matching architecture in universal wrapper - /Library/Ruby/Gems/1.8/gems/sys-uname-0.8.3/lib/sys/uname.bundle
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from /Library/Ruby/Gems/1.8/gems/autotest-fsevent-0.1.1/lib/autotest/fsevent.rb:3
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
from /Users/stevesmith/.autotest:2
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:195:in `load'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:195:in `initialize'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:194:in `each'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:194:in `initialize'
from /Library/Ruby/Gems/1.8/gems/autotest-rails-4.1.0/lib/autotest/rails.rb:7:in `initialize'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:138:in `new'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:138:in `run'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/bin/autotest:55
from /usr/bin/autotest:19:in `load'
from /usr/bin/autotest:19

The gem sys-uname makes use of native extensions but is obviously not reinstalled as autotest is its just assumed to already exist. In this case simply doing

sudo gem uninstall sys-uname
sudo gem install sys-uname

will bring it back. I guess this isn’t the most tricky thing to figure out but I thought I’d post it in case it was of use to anyone.

Fragment Cache Expiration in Ruby on Rails

A few days ago I was trying to implement fragment caching with a timed expiration using memcached in rails. I needed a system to cache the results of querying a large number of rss feeds that were being aggregated on the page. Whilst I could have created a system to do this offline and store the results it was easier to just fragment cache and show a stale cache whilst new results were found.

When manually caching or in action caching you simply pass :expires_in => 5.minutes to tell memcached to automatically expire the page after 5 minutes. However this didn’t seem to work with fragment caching, after a tiny bit of digging and error I realised that in order to specify the options hash you have to first specify a key. Since the key is generally made up of the request url I simply used this and appended a unique id for the fragment. Obvious when you think about it I guess!

cache("#{request.url}?fragment", {:expires_in => 60.seconds}) do