Skip to content
ReCaptcha helpers for Rails apps
Find file
Pull request Compare This branch is 232 commits behind ambethia:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.




Jason L Perry (


Copyright © 2007 Jason L Perry






This plugin adds helpers for the reCAPTCHA API. In your views you can use the recaptcha_tags method to embed the needed javascript, and you can validate in your controllers with verify_recaptcha.

Beforehand you need to configure Recaptcha with your custom private and public key. You may find detailed examples below. Exceptions will be raised if you call these methods and the keys can't be found.

About this fork

This fork tries to introduces a more convenient way to configure recaptcha's settings. The API will be inspired by Thoughtbot’s Hoptoad.

Rails Installation

reCAPTCHA for Rails can be installed as a gem:

config.gem "recaptcha", :lib => "recaptcha/rails"

Or, as a standard rails plugin:

script/plugin install git://

Merb Installation

reCAPTCHA can also be used in a Merb application when installed as a gem:

dependency "alm-recaptcha", ">=", :require_as => "recaptcha/merb"

Initial Merb compatability funded by ALM Labs.

Setting up your API Keys

There are multiple ways to setup your reCAPTCHA API key once you obtain a pair.


You may use the block style configuration. The following code could be placed into a config/initializers/recaptcha.rb when used in a Rails project.

Recaptcha.configure do |config|
  config.public_key  = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
  config.private_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'

This way, you may also set additional options to fit recaptcha into your deployment environment.

Shell environment

Or, you can keep your keys out of your code base by exporting the following environment variables. You might do this in the .profile/rc, or equivalent for the user running your application:

export RECAPTCHA_PUBLIC_KEY  = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
export RECAPTCHA_PRIVATE_KEY = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'

Per call

You can also pass in your keys as options at runtime, for example:

recaptcha_tags :public_key => '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'

and later,

verify_recaptcha :private_key => '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'

This option might be useful, if the same code base is used for multiple reCAPTCHA setups.


Some of the options available:


Uses secure http for captcha widget (default false)


Include <noscript> content (default true)


Takes a hash containing the theme and tabindex options per the API. (default nil)


Render the dynamic AJAX captcha per the API. (default false)


Your public API key, takes precedence over the ENV variable (default nil)


Override the error code returned from the reCAPTCHA API (default nil)

You can also override the html attributes for the sizes of the generated textarea and iframe elements, if CSS isn't your thing. Inspect the source of recaptcha_tags to see these options.


This method returns true or false after processing the parameters from the reCAPTCHA widget. Why isn't this a model validation? Because that violates MVC. Use can use it like this, or how ever you like. Passing in the ActiveRecord object is optional, if you do–and the captcha fails to verify–an error will be added to the object for you to use.

Some of the options available:


Model to set errors


Model attribute to receive errors (default :base)


Custom error message


Your private API key, takes precedence over the ENV variable (default nil).


The number of seconds to wait for reCAPTCHA servers before give up. (default 3)

respond_to do |format|
  if verify_recaptcha(:model => @post, :message => "Oh! It's error with reCAPTCHA!") &&
    # ...
    # ...


  • Remove Rails/ActionController dependencies

  • Framework agnostic

  • Add some helpers to use in before_filter and what not

  • Better documentation

Something went wrong with that request. Please try again.