Using re-captcha in Rails application

Captcha validation is implemented in rails app using “ambethia-recaptcha” gem. After installing the gem you need to do the following…..

You need to register in google recaptcha group which provides you 2 keys and put these 2 keys in your environment.rb file as follows

ENV['RECAPTCHA_PUBLIC_KEY']  = '6LffcrsSA...................................''
ENV['RECAPTCHA_PRIVATE_KEY'] = '6LffcrsSA.................................'

You just need to do the following code in your view if you want to show the default recaptcha…

=recaptcha_tags( )

If you want to customize it then write the following code in your view…

=recaptcha_tags( :display => {:theme => 'custom', :custom_theme_widget => 'recaptcha_container'})

#recaptcha_container

#recaptcha_image

%input#recaptcha_response_field.text{ :name => "recaptcha_response_field", :type => "text" }

%label{ :for => "recaptcha_response_field" }

(Enter the two words above)

%p

Choose captcha format:

%a.btn_img{ :href => "javascript:Recaptcha.switch_type('image');",:title => 'Image' }

or

%a.btn_audio{ :href => "javascript:Recaptcha.switch_type('audio');",:title => 'Audio' }

/%input#recaptcha_reload_btn{ :type => 'image', :src => '/images/home/refresh.gif', :style=>'width: 18px; height:15px',:value => 'New', :onclick => "Recaptcha.reload();" }

%script{ :src => "http://api.recaptcha.net/challenge?k=#{ENV['RECAPTCHA_PUBLIC_KEY']}", :type => "text/javascript" }

%noscript

%iframe{ :src => "http://api.recaptcha.net/noscript?k=#{ENV['RECAPTCHA_PUBLIC_KEY']}" }

/height="300" width="500" frameborder="0">

%textarea{ :name => "recaptcha_challenge_field", :rows => "3", :cols => "40" }

Then for validation you have to write code in your controller..........
if verify_recaptcha(:model => @user, :message => "Oh! It's error with reCAPTCHA!")
//
end