/ devise

Recaptcha setup and usage with devise

recaptcha is a gem that uses google's CAPTCHA service.
A CAPTCHA is a program that can tell whether its user is a human or a computer.

To Setup recaptcha on local machine we need to add the gem,

 gem "recaptcha", :require => "recaptcha/rails" 

then we need to obtain private and public key by registering to this url


While registration we can choose if we need public and private keys which work on
particular domain or we can choose to use it on any domain.

Once we have the public and private key we can create an initializer and
configure them in our application using,

Recaptcha.configure do |config|
  config.public_key  = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
  config.private_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
  config.proxy = 'http://myproxy.com.au:8080'

Once configuration is done we can use the recaptcha_tags in our view using,

 <%= recaptcha_tags %> 

To verify whether the text entered by user matches the captcha, we use the method
"verify_recaptcha" in controller, it basically returns true or false.

respond_to do |format|
  if verify_recaptcha && @post.save

To use recaptcha with devise registration form we need to overide the devise create
method as follows,

  class RegistrationsController < Devise::RegistrationsController
    def create
      if verify_recaptcha
        flash.now[:alert] = "There was an error with the recaptcha code below. Please re-enter the code."
        flash.delete :recaptcha_error
        render :new

The styles for the recaptcha widget are from the gem itself so while integrating for me the
UI was breaking and there were overlapse of text, so to fix them i had to override the
default styles using,

#recaptcha_response_field {
  height: 20px;
td div.recaptcha_input_area {
  height: 0px !important;