Skip to content

Filtering spam with akismet and rails

I’ve got a rails site with an active comments section. For a couple of years I’ve been using the recaptcha gem by fellow tampa.rb member Jason Perry, and that’s mostly worked well.

Recently we’ve been getting hit by spam from Russia which has been getting past the recaptcha filters. The spammer in question may or may not be Leo Tolstoy (pictured).

Has recaptcha been cracked? You can read

Image via Wikipedia

opinions about that elsewhere.

In the meantime, I needed to add more protection to keep the discussions hammy. So, I turned to the akismet service from the WordPress folk, and the rakismet gem.

To get it set up was fairly simple, with one gotcha.

First, install the rakismet gem. In your Gemfile:

gem 'rakismet', '0.4.2'

Note that I’m specifying the 0.4.2 version – this app is on rails 2.3.x, and the 1.0.x version of rakismet is rails 3 only.

Next, sign up for an akismet api key over at This service will run you $5/mo.

Then add your key and site url to config/initializers/rakismet.rb: Rakismet::KEY = '8167608161e2' Rakismet::URL = ''

Now the gotcha: the docs for rakismet hint that a third setting, Rakismet::HOST, is optional, and only useful if you’re using typepad or another akismet endpoint. It fails if that’s not configured, though, and there’s no default in the gem. So, add a third line:

Rakismet::HOST = ''

In your comment model, add:

include Rakismet::Model

You may have to map some fields from your model to the ones rakismet expects; details are in the readme.

Now, before you save a comment, just check


and you’re all set.

You may want to save the comments anyway and set a state flag to ‘spam’ or the like, so you can make sure valid comments aren’t getting unfairly flagged.

Hope that helps.

Enhanced by Zemanta

Post a Comment

Your email is never published nor shared. Required fields are marked *