Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Validates Captcha

Validates Captcha

= CAPTCHA validation

This plugin adds the ability to require CAPTCHA[http://en.wikipedia.org/wiki/Captcha] validation in your model. It depends on RMagick[http://rmagick.rubyforge.org/] to generate images.

= Usage

How to use in your Rails application.

Install

Put this plugin in your application's vendor/plugins directory. This can be done easily using the script/plugin script in your application:

script/plugin install http://svn.2750flesk.com/plugins/trunk/validates_captcha
Prepare

The plugin assumes it has write access to a directory where it will store generated images. This is by default public/images/captcha. You can create this directory by running script/generate captcha image_directory in your application’s root directory. You should check that your web server’s user has write access to this directory.

It also assumes to have write access to a directory where a PStore file with the generated CAPTCHA challenges is stored. This is by default var/data. You can create this directory by running script/generate captcha store_directory in your application’s root folder.

Both of these locations can be changed. See FleskPlugins::CaptchaConfig for details.

Update Model

Put this in the model(s) you want to validate against a CAPTCHA challenge:

class MySuperModel < ActiveRecord::Base
  validates_captcha
end
Update View

In your view, put this inside the form for the model that requires CAPTCHA validation:

<% c = prepare_captcha -%>
<%= captcha_hidden_field c, 'my_super_model' %>
<%= captcha_image_tag c %>
<%= captcha_label 'my_super_model', 'Type in the text from the image above' %>
<%= captcha_text_field 'my_super_model' %>

See AngryMidgetPluginsInc::Helpers::Captcha for options to customise your view.

Update Controller

In your controller, you need to assign the values from your form to your model. If you’re using some form of mass-assignment, this will happen automatically:

def save
  @supermodel = MySuperModel.find(params[:id])
  @supermodel.attributes = params[:my_super_model]
  @supermodel.save
end

The virtual attributes, captcha_id and captcha_validation, are accessible to mass-assignment. If you’re assigning each attribute seperately, you will also have to assign these virtual attributes:

def save
  @supermodel = MySuperModel.find(params[:id])
  @supermodel.name = params:my_super_model
  @supermodel.description params:my_super_model
  @supermodel.captcha_id = params:my_super_model
  @supermodel.captcha_validation = params:my_super_model
end
Restart server

For Rails to use the newly installed plugin, you have to restart the server, even in development mode. You only have to do this once.

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly

Users


See all 12 member details


Membership

+ Join this railsplugin

Record Maintainer

Tore Darell