Fixing ASCII-8BIT encoding for Rails 2.3 ERB

The combination of Ruby 1.9 and Rails is so much fun when it comes to character encoding. Words to adequately describe the sheer joy it gives me are hard to find. They are often four-letter though. Maybe that’s significant, I don’t know.

What I do know is this: ERB in Ruby 1.9 and Rails 2.3 will happily force all your templates to ASCII-8BIT while your database strings are most likely UTF-8. Hilarity ensues. There is no way to change this behaviour besides monkeypatching a bunch of internals.

So without further ado here are the monkeypatches: gist.

Drop in config/initializers and enjoy.

Rails on Ruby 1.9.2: every POST failing?

I ran into a nice problem today. Every POST to a Ruby on Rails on Ruby 1.9.2 app was failing. The error was nasty, too: the dreaded FAILSAFE which tells you nothing. After hunting mostly through Rack I found the culprit.

attachment_fu is not compatible with Passenger 3 on Ruby 1.9.2

Yes I know that’s an edge case, but it was true for me. Consider this code in attachment_fu:

require 'tempfile'

Tempfile.class_eval do
# overwrite so tempfiles use the extension of the basename.  important for rmagick and image science
  def make_tmpname(basename, n)
    ext = nil
    sprintf("%s%d-%d%s", basename.to_s.gsub(/\.\w+$/) { |s| ext = s; '' }, $$, n, ext)
  end
end

Does that make you cry? It should.

Anyway, after removing it completely my app works fine again.