Monday, June 2, 2008

Adding Captcha to your Application

Are you developing an RoR application and want to add CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) Authentication in it?

*Note
1. simple_captcha plugin is being used here if you want to use other plugin, you can find it in the net.
2. You must have RMagick installed. (This is a prerequisite.)

Here's the simple steps in adding a captcha image:

1. Type this command in your app root dir:
>> ruby script/plugin install svn://rubyforge.org/var/svn/expressica/plugins/simple_captcha


2. After successful installation.

>> rake simple_captcha:setup

(Am using Rails 2.x version for < 2.0 do

>> rake simple_captcha:setup_old)


3. Then do the migration.

>> rake db:migrate


4. Add this line of code to you config/routes.rb

map.simple_captcha '/simple_captcha/:action', :controller => 'simple_captcha'


5. Add this line in the file app/controllers/application.rb

include SimpleCaptcha::ControllerHelpers

6. In your view add:

<%= show_simple_captcha(:object=>"user") %>


7. In your model add:
apply_simple_captcha

8. Validating with captcha:
@user.valid_with_captcha?

9. Saving with captcha:
@user
.save_with_captcha

View Options

:label
provides the custom below the image, default is “(type the code from the image)”

:image_style
Provides the specific image style for the captcha image.
There are eight different styles available with the plugin as…

  • simply_blue
  • simply_red
  • simply_green
  • embosed_silver
  • all_black
  • distorted_black
  • charcoal_grey
  • almost_invisible

Default is ’simply_blye’
You can also specify ‘random’ to select the random image style.

:distortion
Handles the complexity of the image. The :distortion can be set to ‘low’, ‘medium’
or ‘high’. Default is ‘low’.

:code_type
Handles the text of the captcha image. Available options are ‘numeric’ or ‘alphabetic’.
Default is ‘alphabetic’.

:object
the name of the object of the model class, to implement the model based captcha.

How to change the CSS for SimpleCaptcha DOM elements ?
You can change the CSS of the SimpleCaptcha DOM elements as per your need in this file…
For Rails >= 2.0 the partial view file wiil reside as…
“/app/views/simple_captcha/_simple_captcha.erb”
For Rails < 2.0 the partial view file will reside as…
“/app/views/simple_captcha/_simple_captcha.rhtml”

View’s Examples

simple_captcha


for detailed info:
http://expressica.com/simple_captcha/

No comments: