Validates Captcha plugin

Plugin details

This plugin adds the ability to require CAPTCHA validation in your model.

Websitehttp://sargon.interinter.net/validates_captcha/ Repositoryhttp://nimrod.interinter.net/plugins/trunk/validates_captcha/ Author Tore Darell Tags Captcha LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install http://nimrod.interinter.net/plugins/trunk/validates_captcha/

Prepare
============

This plugin assumes it has 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. You should check that your web server's user has write access to this directory. If you plan to use the image challenges, these will also need to be stored in a server-writable directory. 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.

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

Update Model
==============

This step is not necessary if you want to validate the challenge in your controller. Instructions on how to do that are further down on the page.

  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 :type => :image -%>
  <%= 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' %>


To use the question challenge instead, do this:

  <% c = prepare_captcha :type => :question -%>
  <%= captcha_hidden_field c, 'my_super_model' %>
  <%= captcha_question_as_label c, 'my_super_model' %>
  <%= captcha_text_field 'my_super_model' %>


See FleskPlugins::Captcha::Helpers for how to customise your view.

Update Controller
================

If you want to validate the challenge in your controller, you can use the +captcha_valid?+ method:

  def save
  	supermodel = MySuperModel.find(params[:id])
  	supermodel.attributes = params[:my_super_model]
  	
  	if captcha_valid?(params[:my_super_model][:captcha_id], params[:my_super_model][:captcha_validation])
  		supermodel.save
  	else
  	  flash[:error] = "Are you sure you're human?"
  	  redirect_or_something
  	end
  end



If you want to let the model handle the validation, 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][:name]
    @supermodel.description params[:my_super_model][:description]
    @supermodel.captcha_id = params[:my_super_model][:captcha_id]
    @supermodel.captcha_validation = params[:my_super_model][:captcha_validation]
  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.

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: hardway, 7 months ago