RMagickCaptcha

RMagickCaptcha is a gem that implements captcha for using in Rails application. The gem provides functionality to create a captcha image and validate user's input.

  • The gem allows multiple captchas at the same page

  • The gem supports I18n and provides locale resource

  • The gem provides rmagick_captcha controller controller to generate images

  • The gem allows …

Quick Start

In your Gemfile:

gem "rmagick_captcha", ">= 0.6.2"

In your model:

# Server create captcha_key, user input into captcha_text
has_rmagick_captcha :captcha
validate :validate_rmagick_captcha

In your controller:

download_rmagick_captcha :show

def new
  @user = User.new
  session[:user_captcha] = @user.reset_captcha_key
end

def create
  @user = User.new(params[:user])
  @user.captcha_key = session[:user_captcha]
  @user.save
  if @user.errors.empty?
    session[:user_captcha] = nil
    redirect_back_or_default('/welcome')
    flash[:notice] = "Thanks for signing up!"
  else
    session[:user_captcha] = @user.reset_captcha_key
    render :action => 'new'
  end
end

In your views:

<% form_for @user, :url => users_path,
   :html => {:id => "user_form", :class => "html-form" } do |f| %>
  <fieldset>
    <ul>
      <li>
        <%= rmagick_captcha_tag(:controller => "user", :id => "user_captcha")  %>
        <%= f.text_field :captcha_text, :maxlength => 16%>
        <%= f.error_message_on :captcha_text %>
      </li>
    </ul>
    <%= f.submit "Submit"%>
  </fieldset>
<% end %>

In your routes.rb:

match 'user(/show)' => 'user#show', :as => :captcha

If you wish:

-Run ruby script/rails generate rmagick_captcha to gen resources such as locale files.
-Use config/initiaizers to configure options for this gem

Installation

  • Type 'gem install –local rmagick_captcha' with root account if you have installed RubyGems.

Example

Example goes here.

Copyright © 2011 arufanov, released under the MIT license.