Veerasundaravel's Ruby on Rails Weblog

May 13, 2011

Google Recaptcha in Rails

Filed under: Gems, Plugin, Ruby, Ruby On Rails — Tags: , , , , , , , , — Veerasundaravel @ 3:43 pm
The logo of reCAPTCHA

Image via Wikipedia



A CAPTCHA is a program that can tell whether its user is a human or a computer. You’ve probably seen them — colorful images with distorted text at the bottom of Web registration forms. CAPTCHAs are used by many websites to prevent abuse from “bots,” or automated programs usually written to generate spam. No computer program can read distorted text as well as humans can, so bots cannot navigate sites protected by CAPTCHAs. reCAPTCHA  is google product and its a better and alternate solution to the existing image captchas. You can read the complete story about Recaptcha using this link.


Lets Kick Start:

In order to use google recaptcha, we need to register our site in google recaptcha site. Refer this link for registerting your application https://www.google.com/recaptcha/admin/create. Refer the screenshot below for further assistance.




Once after entering you domain name, you can click on the Create Key button. Which will create the required API keys and redirect you to the next listing page where you can get the API details as below:






ReCaptcha helpers for Rails apps:

So when you are ready with API keys, you can start the reCAPTCHA integration into you Rails application. We have rails Plugin called recaptcha, it is more useful and easier for the integration with Rails.

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.

See the RDOC documentation for more information on usage.


Rails Installation:

reCAPTCHA for Rails can be installed as a gem:

                       config.gem “ambethia-recaptcha”, :lib => “recaptcha/rails”, :source => “http://gems.github.com”

Or, as a standard rails plugin:

                       script/plugin install git://github.com/ambethia/recaptcha.git


Setting up your API Keys:

There are two ways to setup your reCAPTCHA API keys . You can pass in your keys as options at runtime, for example:

                       recaptcha_tags :public_key => ‘6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy’

and later,

                       verify_recaptcha :private_key => ‘6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx’

Or, preferably, you can keep your keys out of your code base by exporting the environment variables mentioned earlier. 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’

If that‘s not your thing, and dropping things into config/environment.rb is, you can just do:

                       ENV[‘RECAPTCHA_PUBLIC_KEY’]  = ‘6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy’ ENV[‘RECAPTCHA_PRIVATE_KEY’] = ‘6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx’


Displaying reCAPTCHA form:

Recaptcha gem offers you a helper method called recaptcha_tags which can display the reCaptcha form for you in your view files as follows:

Some of the options available with recaptcha_tags:

:ssl ==>    Uses secure http for captcha widget (default false)
:noscript ==>    Include <noscript> content (default true)
:display ==> Takes a hash containing the theme and tabindex options per the API. (default nil)
:ajax ==>    Render the dynamic AJAX captcha per the API. (default false)
:public_key ==> Your public API key, takes precedence over the ENV variable (default nil)
:error ==>    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.


Verifying ReCaptcha:

Using verify_recaptcha method you can easily find out the correct input from user for the displayed captcha.

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 ==>    Model to set errors
:message ==> Custom error message
:private_key ==> Your private API key, takes precedence over the ENV variable (default nil).
:timeout ==> 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!’) && @post.save
        # …
    else
        # …
    end
end


February 15, 2010

Rails3 Beta installation

This post will explain how I proceeded with latest beta version Rails3 and the issues I faced.

My current version of rails and ruby:

veera@client25:~$ rails -v
Rails 2.3.5
veera@client25:~$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
veera@client25:~$

trying to install beta version of rails3.

gem install tzinfo builder i18n memcache-client rack  rake rack-test rack-mount erubis mail text-format thor bundler
gem install rails –pre

I forget to add sudo, so that I got so many errors. Let me try with sudo.

Ok some what better am getting error message only while installing generating documentation for builder-2.1.2

gem install rails –pre – was not successfull. As it haven’t installed rails3 version as I expected.

Getting error like below:

WARNING:  RubyGems 1.2+ index not found for:

RubyGems will revert to legacy indexes degrading performance.
Updating metadata for 1 gems from http://gems.rubyforge.org/

surfed google for the error, http://bparanj.blogspot.com/2008/11/warning-rubygems-12-index-not-found-for.html

lemme try the idea mentioned in that

veera@client25:~$ sudo gem sources -a http://gems.github.com
http://gems.github.com added to sources
veera@client25:~$

or else remove all the gem sources from you location, so that it can any of the source directly from net

I assume installing sudo gem install rack-mount –version=0.4.0, solved my issue.

Now I can see that rails3 is installed in my machine.

after this try the following options:

$ sudo gem install tzinfo builder memcache-client rack rack-test erubis mail text-format bundler thor i18n
$ sudo gem install rack-mount –version=0.4.0
$ sudo gem install rails –pre
$ rails -v
Rails 3.0.0.beta

then create the app in rails3 version

rails RailsBeta3

I can find a new file application.rb under config directory. Seems we have to use it instead of environment.rb

* Issues found:

1. Couldn’t find support for postgresql with Rails3.
2. Not able to start the rails app using mongrel_rails start.

My initial scaffold is working fine. rails generate scaffold group title:string description:text

Referred http://www.slideshare.net/GreggPollack/rails-3-beautiful-code-3219240 for the updated methods and options in Rails3.l,
Cool, simply and effective slide show about Rails3. Some of the important points from that:

1. ruby script/* is modified as rails(ex ruby script/server -> rails)
2. amazing and simplified routes. map.resources is modified as resources and lot more.
3. Improved syntax for respond_to method.
4. Simplified action mailer option.
5. Active record syntax is also modified, Person.all(:conditions=>”blah blah) to Person.where(“blah blah”)
6. form_remote_tag, link_to_remote tag is also simplified and the respective html source is also looks very simple. (no more onclick”new Ajax request ………..”)

looking for few more documents for the modified methods in rails3.

Tried to generate a gem in Rails3.

restful-authentication plugin is not working in rails – May be auth_logic plugin will work.

Grrrrrrr, I can’t create a older version of rails app  – rails _2.3.5_ blog235

/usr/local/lib/site_ruby/1.8/rubygems.rb:827:in `report_activate_error’: RubyGem version error: railties(3.0.0.beta not = 2.3.5) (Gem::LoadError)
from /usr/local/lib/site_ruby/1.8/rubygems.rb:261:in `activate’
from /usr/local/lib/site_ruby/1.8/rubygems.rb:68:in `gem’
from /usr/bin/rails:18

Hope rails3 alpha version should have fix for this. So now am reverting back to old version of rails by simply uninstalling all the gems related to rails3.

November 23, 2008

Difference between Ruby Gems and Rails Plugins

Filed under: Ruby — Tags: , , , , , , , — Veerasundaravel @ 12:40 pm

Here some quick differentiation between RubyGems and RailsPlugin. They both offer out-of-the-box, third party functionality. While Ruby gems and Rails plugins have similar effects in your application, they obviously vary significantly in their architecture. Ultimately, each will prove more or less useful in a given scenario.

Ruby Gems:

1. Gem is a packaged ruby application using the packaging system defined by RubyGems.
2. Gems are highly portable chunks of Ruby code that can be used inside any Ruby script or application(system wide).
3. Rails itself is a Gem.
4. Easily we can install, upgrade and query gem version.
5. Installation syntax: gem install gem_name.

Rails Plugin:

1. Plugin is an extension of Rails Framework.
2. All plugins are loaded from vendor/plugins.
3. Can not be upgraded by using a command. To upgrade one have to uninstall and then install upgraded version.
4. Has to be hooked into rails application. (has to have init.rb)
5. Should have an install.rb file.
6. We can’t use a plugin system wide, can be used application wide only.
7. Installation syntax: ruby script/plugin install plugin_name

Useful references for Gems:

https://rubygems.org/
http://ruby-toolbox.com/

Useful references for Plugins:

http://agilewebdevelopment.com/
https://github.com/rails/

%d bloggers like this: