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://github.com/ambethia/recaptcha.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!
22 comments:
This is simply beautiful. Love it!
I'm glad you like it. Go, Recaptcha, go!
word up to the rails brutha, thanks man for the details
Step 3 is currently blank. Can you put that back up? Thanks!
That must have been a blogger.com hiccup, cause I didn't change anything! Seems to be OK now...
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??
Nice guide, keep it up!
Any reason why you are saving the model twice?
--code--
if verify_recaptcha(@thing) && @thing.save!
@thing.save!
--code--
Ron,
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://github.com/ambethia/recaptcha.git
Cheers!
I wish I would have found this sooner. Excellent Job, saved half of the hair on my head!
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.
nice writeup for the very nice plugin
made my day
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 ...
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!
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!
yay ! smooth as fuck !!!!!
I can't make it work with the Sinatra framework.
recaptcha_tags works fine, but verify_recaptcha is problematic.
Help please? Thanks!
is it work for localhost?!?
is it work in localhost?
You've made it very simple and straight forward. Thank you very much. It indeed helped after trouble with rmagick and the like.
thanks very much from the egyptian programmer
are there any other captcha plugin other than ReCaptcha and Simple Captcha( I think this is the one using ImageMagick/RMagick )?
Thanks
Post a Comment