It was an early morning here at the apartment and I was just getting ready to sneak in a few levels of Call of Duty 3 before the girlfriend awakened. Unfortunately for me, I checked my email before I picked up the XBox360 controller only to find that random comments were being added to various entries on the website. You guessed it, comment spam and the kind that points you towards those sites only adults should visit. I knew it was only a matter of time, but why the 4th of July? Oh well, I've been meaning to implement captchas for sometime now and courtesy of a variety of spam friendly ip addresses have finally gotten around to spending a few lovely minutes with Rails to do so. Some of you may think this was just another day of me banging my fingers against the keyboard, but no, I had other things planned for the day. Uh yeah, fireworks!!! Needless to say, I'd had to implement this quick before my inbox was filled with even more spam-a-lishess email messages. For those of you unfamiliar with the term captcha, it stands for "Completely Automated Public Turing test to tell Computers and Humans Apart". Say that five times fast and you get a gold star.
Purposely trying to not re-invent the wheel, I searched around for already existing solutions and discovered Simple Captcha. Reviewing the installation and usage instructions, it appeared to be a viable solution given my needs at the time. Thus, I installed and configured it...
Install the plugin
svn co svn://rubyforge.org/var/svn/expressica/plugins/simple_captcha simple_captcha
Implement it in the views
<%= show_simple_captcha(:label => "Captcha:", :image_style => "random", :distortion => "low") %>
Bear in mind, I only wanted this captcha to be required for supporting anonymous comments. Users with accounts and that were logged in would not be required to use the captcha (at least not yet).
Update the controller methods
# Test to make sure the user is human.
def comment
if simple_captcha_valid? or session[:user]
valid_captcha = true
end
if valid_captcha and @article.article_comments << @comment
[SAVE COMMENT CODE]
else
[LOG COMMENT AND RETURN ERROR]
end
end
I decided to use the controller approach to implement the captcha as opposed to the model-based method. It wasn't clear as to how I might use the model-based method when implementing it for object collections.
So there you have it, a simple captcha for helping to better authenticate humans from bionic spam bots. I apologize for having to introduce another step in the posting process to those of you that are legitimate anonymous commenters and hope that it won't stop you from sharing you feedback. Questions, suggestions? You know where to send them.

