---
title: "验证码生成插件simple_captcha"
author: "fireflyman"
date: "2011-01-12T23:37:29Z"
tags: []
categories: ["编程语言"]
slug: "simple-captcha"
draft: false
lastmod: "2011-01-12T23:37:29Z"
id: 869838
url: "https://fireflyman.iteye.com/blog/869838"
---

插件地址:<https://github.com/eshopworks/simple_captcha>

**(1)入门篇:**
安装插件-->

```Ruby
script/plugin install git://github.com/eshopworks/simple_captcha.git
```


执行-->

```Ruby
rake simple_captcha:setup
```


在routes.rb中添加-->

```Ruby
map.simple_captcha '/simple_captcha/:action', :controller => 'simple_captcha'
```


你还需要在application_controller.rb中加入-->

```Ruby
ApplicationController < ActionController::Base
  include SimpleCaptcha::ControllerHelpers
end

```


根据我的项目,需要在app/views/events/new.html.erb的form中加上-->

```Ruby
 <p>
    <%= show_simple_captcha(:object => 'event') %>
 </p>
```


在app/models/event.rb中添加-->


```Ruby
class Event < ActiveRecord::Base
  apply_simple_captcha
end
```


将app/controllers/events_controller.rb中的create action-->

```Ruby
if @event.save
```


改为-->

```Ruby
if @event.save_with_captcha
```


**(2)进阶篇**
在插件下载的Wiki里,有一个链接,是关于此插件的详细用法的,如下-->
Simple Captcha:<http://expressica.com/simple_captcha/>

如需要进阶使用,可以根据里面的说明进行相关修改,如->

> :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.


看看我的应用-->
在user.rb中

```Ruby
apply_simple_captcha :message => "image and text were different", :add_to_base => true
```


在对应的new.html.erb里

```Ruby
<p>验证码</p>
<%= show_simple_captcha(:object => 'user', :code_type => 'numeric', :image_style => 'all_black', :distortion => 'low',:label => "(请正确输入图片中的数字?)") %>
```


因此它看起来应该是这样的-->
![](http://dl.iteye.com/upload/picture/pic/79903/fa4a5dc5-6952-3a60-acdc-09b28445ebdc.jpg)

**(3)实操篇:**
从上图可以看到,这种样式其实是很丑的.而且也没有刷新验证码的按钮.反正就是不顺眼.废话不说了,看看netfork兄的文章吧.
验证码生成换成simple_captcha了:<http://netfork.iteye.com/blog/434099>

以上东东只供记录.