ReCaptcha is a powerful captcha service to be use with a form to stop spam. This blog demonstrates the use of reCaptcha in rails application. Here I have used the ruby version 1.9.3 and Rails version 3.2.8.
Environment
- Ruby version – 1.9.3
- Rails – 3.2.8
Step#1
Create an account with Recaptcha “http://www.google.com/recaptcha” and register your site name with your google account to retrieve the public and private key that will be used later on the application
Step#2
Installation of Recaptcha
- Gem
- Open your Gemfile and add this code
gem 'recaptcha', :require => 'recaptcha/rails'
- Plugin
- rails plugin install git://github.com/ambethia/recaptcha.git
Step#3
Create a file named recaptcha.rb in RAILS_ROOT/config/initializer and fill it with these codes
ENV['RECAPTCHA_PUBLIC_KEY'] = 'youractualpublickey' ENV['RECAPTCHA_PRIVATE_KEY'] = 'youractualprivatekey'
Step#4
Usages Open your views file and add this code
<%= recaptcha_tags %>
Step#5
Validation In controller, you can check the captcha validation by the following
if @model.save && verify_recaptcha(:model => @developer, :message => "Oh! It's error with reCAPTCHA!") #captcha is valid else #captcha is invalid end