<?xml version="1.0" encoding="UTF-8"?>
<page>
  <body>&quot;API&quot;:http://sargon.interinter.net/validates_captcha/ &quot;SVN&quot;:http://nimrod.interinter.net/plugins/trunk/validates_captcha

ValidatesCaptcha provides CAPTCHA for your Rails application, if you really, really need it. It has two types of &quot;challenges&quot; built-in, the familiar image challenge and the increasingly familiar logic challenge. The latter is the better choice, because the image challenge is much more inaccessible, and it depends on &quot;RMagick&quot;:http://rmagick.rubyforge.org/. This plugin is a little outdated, but I guess it still works as well as it ever has.

h3. Quick how-to

There's a few different things you can customise, but you can read about that in the &quot;API reference&quot;:http://sargon.interinter.net/validates_captcha/, so I'll keep this short and simple and only go through setting up the logic challenge.

Before starting, install the plugin with the _script/plugin_ script in your @RAILS_ROOT@.

[terminal]
script/plugin install http://nimrod.interinter.net/plugins/trunk/validates_captcha
[/terminal]

h3. Prepare application

After that, the first thing you need to do is make sure there's a writable directory where the challenges can be stored in a PStore file (no, it doesn't scale well :-). You can create this with the built-in generator:

[terminal]
script/generate captcha store_directory
[/terminal]

Then generate a _captcha.yml_ configuration file:

[terminal]
script/generate captcha config
[/terminal]

The generated config file should be pretty easy to figure out just by reading it.

h3. Model

[ruby]
class Comment &lt; ActiveRecord::Base
  validates_captcha
end
[/ruby]

h3. View

[rhtml]
&lt;% c = prepare_captcha :type =&gt; :question -%&gt;
&lt;%= captcha_hidden_field c, 'comment' %&gt;
&lt;%= captcha_question_as_label c, 'comment' %&gt;
&lt;%= captcha_text_field 'comment' %&gt;
[/rhtml]

h3. Controller

[ruby]
def create
  comment = Comment.new(params[:comment])

  if comment.save #Captcha passed
    flash[:notice] = 'Comment was saved'
    redirect_to comments_url
  else #Captcha not passed, or other validation error
    flash[:error] = 'Could not save comment'
    redirect_to new_comment_url
  end
end
[/ruby]</body>
  <created-at type="datetime">2007-04-13T23:13:24-04:00</created-at>
  <id type="integer">3</id>
  <parent-id type="integer" nil="true"></parent-id>
  <published type="boolean">true</published>
  <slug>validates_captcha</slug>
  <summary></summary>
  <title>ValidatesCaptcha</title>
  <updated-at type="datetime">2008-04-18T12:04:27-04:00</updated-at>
</page>
