Friday, February 01, 2008

Recaptcha On Rails

UPDATED Oct. 15, 2009 with most current info

I hate implementing captchas. You know, those annoying things that your users think are deciphering hieroglyphics. I have hated them because I do not want to install a whole massive assemblage of software like ImageMagick/RMagick just to provide some basic protection from spam and bots.

To avoid this, I had hacked together a simple implementation using some free public captcha. However, a client recently turned on SSL on their signup page, and my free public captcha gave a nasty error cause of the domain name in the SSL certificate not matching the domain of their site. How cheap!

I needed a new solution. I waded thru pages of search results with the dreaded RMagick sotuions. But then, lo and behold, I discovered there is a fantastic simple plugin for Ruby on Rails from Ambethia that supports the marvelous Recaptcha.

Recaptcha is a really neat project that uses crowdsourcing to implement a form of human OCR on old books that defy machine OCR. They need human scanners, and we need proof that the people signing up on the site are human. Perfect match!

Less than 10 minutes later I had the plugin installed, implemented, and deployed in production. It is that easy. Here is a four step process that will have you up in moments:

Step 1 - Install the Recaptcha plugin

script/plugin install git://

'Nuff said.

Step 2 - Set Your Recaptcha Account Data in Rails
OK, so I lied. You need to tell the plugin what your account info is, and if you do not yet have a free Recaptcha account, you will need to sign up for one. Once you do, save your keys into config/initializers/recaptcha.rb

ENV['RECAPTCHA_PUBLIC_KEY'] = 'youractualpublickey'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'youractualprivatekey'

Step 3 - Display The Recaptcha Widget In Your View
You will need to display the Recaptcha widget somewhere. In my case, it was the signup page for the site. Just put this view helper where you want the widget to appear:

<%= recaptcha_tags %>

If you are using SSL, use this instead:

<%= recaptcha_tags :ssl => true %>

Step 4 - Validate The Recaptcha
You need to make sure that the user entered the proper values for the Recaptcha. Just add this to the controller action that the view submits to:

If you want to display any Recaptcha errors right along with any model errors of the model you are trying to control adding using the Recaptcha, you could do this instead:

You may have guessed by now that it took me longer to write this posting than to implement Recaptcha using the Ambethia plugin. Hey, that is just the kind of guy I am. It was just so cool, and such a quick solution to a common problem, that I couldn't help myself.

Let the digitizing begin!


Laiq said...

This is simply beautiful. Love it!

Ron Evans said...

I'm glad you like it. Go, Recaptcha, go!

Yahya said...

word up to the rails brutha, thanks man for the details

brandon said...

Step 3 is currently blank. Can you put that back up? Thanks!

Ron Evans said...

That must have been a hiccup, cause I didn't change anything! Seems to be OK now...

SnowmanInTheSun said...

have you figured out a way to render recaptcha on the same page using link_to_remote?? or using any ajax call for that matter??

tadejm said...

Nice guide, keep it up!

Navin Samuel said...

Any reason why you are saving the model twice?

if verify_recaptcha(@thing) &&!!

Jason Morrison said...


Thanks for writing this, quick though it was! Another note: the plugin has moved to github, so the updated installation command is:

script/plugin install git://


johnsonch said...

I wish I would have found this sooner. Excellent Job, saved half of the hair on my head!

ganesh prasad said...

excellent one!
but the captcha looks very big. is it possiblet to modify the plugin to just show image and type words, so that the captcha looks small.

Alfonso said...

nice writeup for the very nice plugin

made my day

JDS said...

Step 3 (which is trouble for me) is blank again... Here's the source:
... you want the widget to appear:<br /><pre><br /><br /></pre><br />If you are using SSL ...

Ron Evans said...

I have updated this post with the latest info for Rails 2.3.x, and used a few other suggestions from commenters.

Thanks everyone for helping improve these instructions!

kenderson said...

Awesome Post! One thing good to note - If you planning on using the Recaptcha plugin inside of a jQuery Facebox, you need to set the ajax option equal to true (= recaptcha_tags(:ajax => true)). Thanks!

bbnnt said...

yay ! smooth as fuck !!!!!

Petero said...

I can't make it work with the Sinatra framework.

recaptcha_tags works fine, but verify_recaptcha is problematic.

Help please? Thanks!

gemblonk said...

is it work for localhost?!?

gemblonk said...

is it work in localhost?

Michael & Annie Nissim said...

You've made it very simple and straight forward. Thank you very much. It indeed helped after trouble with rmagick and the like.

waleed abdalla abdalla nsr said...

thanks very much from the egyptian programmer

aj said...

are there any other captcha plugin other than ReCaptcha and Simple Captcha( I think this is the one using ImageMagick/RMagick )?