Make Rails 2.3.x happy with Ruby 1.9.3

Rails 2.3 + Ruby 1.9.3If you’re like me, you still have some Rails 2.3.x apps hanging around. Yeah, Rails 3 is awesome in some respects, but that “2x performance increase” never quite materialized.

In fact, for the most part, Rails 3 is still slower than Rails 2.3. SIGH!

Nevertheless, Ruby itself is progressing nicely. Ruby 1.9.3-p0 was released a few months ago. With faster garbage collection, startup, and file load times, Ruby 1.9.3 is where it’s at.

The only question is: What do you do to make Rails 2.3 play nicely with Ruby 1.9.3?

It’s actually not that hard!

(more…)

Fixing the Rails 3 Fragment Cache Path

Let’s say you were using Rails 2.3.x, and you made the (wise) decision to implement heavy fragment caching. Then let’s say that you updated to Rails 3.x. At that point, you probably noticed that Rails 3 does something seriously annoying with the paths where it caches fragments.

In Rails 2, doing this:

<% cache('posts/123/123456/main') do %>

Gave you a cache file that looks like this:

RAILS_ROOT/tmp/cache/views/posts/123/123456/main.cache

However, in Rails 3, the same cache code results in a cache file that looks like this:

RAILS_ROOT/tmp/cache/925/AB2/posts%2F123%2F123456%2Fmain

WTH? That’s not documented in the Rails 3 API. So what’s going on, and how do you make Rails 3’s fragment caching work like it used to instead of using those crazy hash subdirectories? Read on!

(more…)

Why You Need Sprites on Your Web Site

In the olden days, video games relied heavily upon sprites given the rather severe limitations of computing hardware at the time.

You can use this “old” technique to reduce the bandwidth load on your server.

Most major web sites use them, but you may never have even heard of a sprite.

But what exactly is an image sprite, and why does it save bandwidth?

(more…)

How to Make Your Rails 2.3 App Work with Ruby 1.9

Ruby 1.9 + Rails 2.3If you’re like me, you’ve got a Rails 2.3.x application running on Ruby 1.8.x – or perhaps Ruby Enterprise Edition. Well, that’s all fine and dandy, but Ruby 1.9.2 is about twice as fast as even REE 1.8.7. So, you’ll probably want to upgrade to the new version of Ruby.

Problem is, you upgrade Ruby and then your Rails 2.3 app starts whining about character encoding problems. Heck, it might not even run! So you search the net in vain trying to find a simple, step-by-step explanation of what you need to do to make your app run with the speedy 1.9.x version of Ruby. And, you pretty much come up empty handed.

Until now!

Here I shall endeavor to outline all the steps necessary to install and convert your Rails 2.3.x app to work flawlessly with Ruby 1.9.x.

(more…)

Scottie’s SuperDuper CSS Drop Shadow

Okay, I know this one has been done like 8 bazillion times, but this method of creating a drop shadow on elements in a web page via CSS and a 1 pixel by 1 pixel transparent PNG file actually DOES work.

You see, I tried all kinds of different methods of creating drop shadows in CSS, both with and without an image file, and there was always a problem. It would render properly in certain browsers. It would render properly, but it only worked for fixed-width elements on the page. Or, maybe it would only render properly for variable width page elements. Or, the darn thing just didn’t work at all.

Well, I got tired of all of that, so I decided to make my own version of the CSS drop shadow. It may not be the prettiest, but it works.

(more…)

How to Make Rails and PHP Apps Fully UTF-8 Compliant with MySQL

Every web programmer knows that UTF-8 is where it’s at these days. Even if you are coding something for only the US market, you’re still probably going to run into names, addresses, or whatever with accented characters.

Everyone knows that “all you have to do” is make your app speak UTF-8, set the character encoding in your web pages to UTF-8, and set your database encoding and collation to something like utf8_unicode_ci, and you’re off and running… right?

Not quite. You’ve probably run into the situation where you hop into phpMyAdmin and edit some table row, and what to your wondering eyes should appear, but something like this:

Voici un article français.

Well, crap. That’s supposed to be a “ç” in “français”, not an “ç”!

And yet, when you view the web page and edit the content in your app, it looks and works fine! What’s going on here?

It’s really quite simple, and apparently something that no one bothers to mention: you have to tell your app to communicate with the database in UTF-8! Here’s how you do that in PHP and Rails

(more…)

Adding Videos, JavaScript, and Other Goodies to Posts in WordPress MU

Some of you may recall my earlier post Adding JavaScript to WordPress Posts without a Plugin. In that post, I talked about how to add JavaScript for, say, embedding a video without having WordPress “sanitize” your code… and without any plugins!

WordPress MU, the version of WordPress that allows multiple blogs to be run under one WP installation, uses a different content sanitizer. In fact, it’s so seriously anal that it blows away not only JavaScript, but all kinds of other HTML tags in your posts that the regular WP leaves untouched! Bad juju.

In the case of WP MU, the best solution that I could find does include a plugin – but you can write it yourself in about 10 seconds. In writing the plugin yourself, you will also know how to maintain it yourself. Even if you don’t know much about PHP, this one’s a piece of cake!

So, here’s how to convince WordPress MU to play nice with the HTML in your posts…

(more…)

Installing ImageMagick from Source on Ubuntu

I like Ubuntu Server. I really like apt-get, because I don’t have to do any work at all to install stuff. It’s a piece of cake.

Unfortunately, sometimes installing using apt-get doesn’t work, or maybe the software I need isn’t the latest and greatest version on the Ubuntu repository. In such cases, there’s only one option left: installing from source.

That means downloading, extracting, configuring, building, debugging, etc. Most people shy away from doing this because it isn’t fast and easy, and stuff can break.

I recently had the opportunity to install the latest version of ImageMagick on an Ubuntu server, and it was a lot easier than I thought it would be!

Here’s how you do it…

(more…)

Avoid the Number 1 CSS Mistake: a, a:visited

I used to hate CSS. I mean, I really, really hated working with CSS at all. Then, one day, I was forced to actually learn it well to design a new web site. It was rough, but I survived.

Don’t ask me why learning CSS was so painful for me, because I can program in a bazillion different languages. For whatever reason, it was just painful. I emerged victorious, however, with the help of a lot of examples I found by searching online.

There was one little problem: the vast majority of examples I found included a little bit of code that will royally screw up all your hyperlinks. So, this post is about how to avoid the #1 CSS mistake: a, a:visited.

(more…)

Fixing the “ActionMailer: Hostname not match server certificate” Error

If you have a Rails app that sends e-mails, you are probably using ActionMailer. Unfortunately, starting with Rails 2.2.2, you may have encountered a wonderfully annoying little error that looks like this:

OpenSSL::SSL::SSLError (hostname was not match with the server certificate):
   /usr/lib/ruby/1.8/openssl/ssl.rb:123:in `post_connection_check'
   /usr/lib/ruby/1.8/net/smtp.rb:582:in `tlsconnect'
   /usr/lib/ruby/1.8/net/smtp.rb:562:in `do_start'
   /usr/lib/ruby/1.8/net/smtp.rb:525:in `start'
   [...]

The are quite a few sites out there that give monkey patches for this problem, but those aren’t very useful because the next time you upgrade Rails, the monkey patch gets obliterated and you’re back to square 1. And then you have to remember how you monkeyed with it the last time to get it working again. Ug…

Instead, fix it the right way!

(more…)