Friday 6 September 2013

Rails Captcha Plugin Usage


     CAPTCHA stands for Completely Automated Public Turing Test to Tell Computers and Humans Apart. Captcha code keeps machines and humans apart. You must have seen the captcha verification while filling sign up forms, posting comments, filling surveys, placing orders, e-commerce transactions, filling feedback forms and many more. Captcha verification is the way to save the website from Spam attack,malicious attack, computer programs used to fill the forms and other malicious activities.

Captcha code verification has the following advantages.
  • Prevention against fake comment spams which are posted on some blog.
  • Avoids thousands of sign up on free email services via bots.
  • Assures that only humans can fill the feedback and survey forms.
  • Prevents the attack of machine programs to break the passwords.

These Plugin generates the random images and random text written
over the images. Captcha plugin takes the help of MiniMagick to create
the images with text. So mini_magick should be installed on your
machine. 

If you don't have mini_magick on your system, use following
command to install mini_magick.

sudo gem install mini_magick

Captcha Plugin Installation Process.

Just run following command from application root directory.

script/plugin install git://github.com/hokam/captcha.git

Captcha Plugin Uninstallation Process.

Just run following command from application root directory to
remove captcha plugin.

script/plugin remove captcha


How to Use Captcha plugin ?


After installation process you need to add 'before_filter' callback in your controller to create captcha image before the processing of specified actions.

Suppose that you have a “new” action in your “CommentsController”
for which you want to create capthca  image, so just add 
“before_filter” at the top of your "CommentsController" for 
“new” action. 

before_filter :create_captcha, :only => [:new]

And add the following line in your form to include captcha image.

<%= image_captcha_tag %>

This tag generates an image with text, a link to change the captcha
image and a text box for the input of captcha text written over the
image.

And for validation of captcha code you need to call validate_captcha
helper in your validation action. Lets say in "create" action of "CommentsController" you want to validate the captcha code, so
you have to do the following modification in your "create" action.

class CommentsController << ActionController::Base
    before_filter :create_captcha, :only => [:new]
    def new
        # action specific data
    end
    # other actions
   
    def create
        if validate_captcha
            # .......
        else
            # ........
        end
    end
end

If captcha validation fails then captcha plugin set the error message
in flash[:notice] variable, so you should use flash[:notice] variable in
your form for the notification of captcha failure message.

<%= flash[:notice] if flash[:notice] %>

So by using this plugin, ruby application can be easily prevented from
the fake form fillings, thousands of sign up in a minutes via bots,
password hacking by machine programs, spam attacks and malicious
attacks.

No comments:

Post a Comment