Integration of Google reCaptcha with Rails application
October 10, 2012
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
- Ruby version – 1.9.3
- Rails – 3.2.8
Create an account with recaptcha “http://www.google.com/recaptcha” and register your site name with your google account to retrieve public and private key that will be used later on the application
Installation of recaptcha
- Open your Gemfile and add this code
gem 'recaptcha', :require => 'recaptcha/rails'
- rails plugin install git://github.com/ambethia/recaptcha.git
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'
Usages Open your views file and add this code
<%= recaptcha_tags %>
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