Clone wiki

ruby-recaptcha / Home


The basics

The ReCaptcha::Client abstracts the ReCaptcha API for use in Rails Applications


  gem install ruby-recaptcha

with Rails

In recent versions of Rails, you can specify the gem in environment.rb:

  config.gem 'ruby-recaptcha'

Demonstration of usage


First, create an account at

Get your keys, and make them available as constants in your application. You can do this however you want, but RCC_PUB, RCC_PRIV (for regular reCaptcha) and MH_PUB, MH_PRIV (for MailHide) must be set to their respective values (the keys you receive from reCaptcha).

It doesn't matter how you set these constants. You can create them however you like:

RCC_PUB='foo' #normal constant decl
RCC_PUB=ENV['i_keep_it_here'] #from an environment variable
RCC_PUB, RCC_PRIV = (*'/etc/keys', 'r').read_lines).map(&:reverse) #stored backwards, in a file.  muaahah!

The ReCaptcha::Client constructor can also take an options hash containing keys thusly:>'some public key', :rcc_priv=>'some private key')

After your keys are configured, and the gem is loaded, include the ReCaptcha::AppHelper module in your ApplicationController:

  class ApplicationController < ActionController::Base
	include ReCaptcha::AppHelper

This will mix-in validate_recap method.

Then, in the controller where you want to do the validation, chain validate_recap() into your regular validation:

  def create
      @user =[:user])
      if validate_recap(params, @user.errors) &&
          # stuff...

Require and include the view helper in your application_helper:

  include ReCaptcha::ViewHelper

This will mix get_captcha() into your view helper.

Now you can just call get_captcha() in your view to insert the requisite widget from ReCaptcha.

To customize theme and tabindex of the widget, you can include an options hash:

  get_captcha(:options => {:theme => 'white', :tabindex => 10})

See the ReCaptcha API Documentation under "Look and Feel Customization" for more information.

Proxy support

If your rails application requires the use of a proxy, set proxy_host into your environment:


Mail Hide

When you mix in ViewHelper as above, you also get mail_hide(address, contents), which you can call in your view thusly:

  <%= mail_hide( %>

Contents defaults to the first few characters of the email address.


If you want to run automated integration testing and get past your calls to validate_recap, you can redefine it in config/environments/test.rb, like so:

config.after_initialize do 
  module ReCaptcha::AppHelper
    def validate_recap( p, errors, options = {})
      return true

Now you can get past user/create in your cucumber stories!


Issue Tracker


Mercurial Repository

Note the wiki & forum & such there...


This code is free to use under the terms of the MIT License.


Comments are welcome. Send an email to the author


  • Victor Cosby (test cleanup, additional code to style widget)
  • Ronald Schroeter (proxy support suggestion & proto-patch)
  • Peter Vandenberk (multi-key support, ssl support, various unit tests, test refactoring)
  • Kim Griggs (found long address-newline bug)
  • Brett Carter (updates for new google apis)
  • David Czarnecki Ruby 1.9 support