用Rails Captcha插件,轻松实现验证码

 Simple Captcha 插件

可以帮我们在Rails 中轻松的实现验证码功能。

并且,他所提供的选项足以满足我们的验证要求,使用简单。支持图片和数字验证。

   

下载

下载地址:Download the archive

 

放入plugins中。

 

设置

 

第一步

 

如果是rails 2.0 以上

 

Ruby代码
  1. <strong>rake simple_captcha:setup  </strong>  

 

如果是rails 2.0 以下

 

Ruby代码
  1. <strong>rake simple_captcha:setup_old  </strong>  

 

第二步

 

迁移数据库

 

Ruby代码
  1. <strong>rake db:migrate</strong>  

 

第三步

 

添加下列代码到config/routes.rb

 

Ruby代码
  1. <strong>map.simple_captcha '/simple_captcha/:action':controller => 'simple_captcha'    
  2. </strong>  

  

第四步

 

添加下面一行到 app/controllers/applications.rb

 

Ruby代码
  1. <strong>ApplicationController < ActionController::Base     
  2.   include SimpleCaptcha::ControllerHelpers     
  3. end  </strong>  

 

使用

 

在view 文件中放入下面的tags代码

 

Ruby代码
  1. <strong><%= show_simple_captcha %>    
  2. </strong>  

 

添加controller 的 action ,如:

 

Ruby代码
  1. <strong>def my_action     
  2.   if simple_captcha_valid?     
  3.     do_this     
  4.   else    
  5.     do_that     
  6.   end    
  7. end </strong>  

 

 

参数和

 

view 参数

 

:label
用于显示在验证码图片旁边的文字提示

 

:image_style
图版样式

 

  • simply_blue
  • simply_red
  • simply_green
  • embosed_silver
  • all_black
  • distorted_black
  • charcoal_grey
  • almost_invisible
  •  

    默认是 'simply_blue'' 

     

    :distortion

    操纵图片的扭曲度,有'low', 'medium', 'high' 三个选项

     

    默认是 'low'

     

    :code_type
    代码类型,有数字和图片两种类型可选。 'numeric', 'alphabetic'

     

    默认是'alphabetic'

     

    :object

    与model class绑定时,所对应的名字

     

    如何改变SimpleCaptcha DOM 的css?

    2.0以上的,请修改 /app/views/simple_captcha/_simple_captcha.erb 文件

    2.0以下的,请修改 /app/views/simple_captcha/_simple_captcha.rhtml 文件

     

    ===============================

    想了解更多,请阅读原文:

     

    http://expressica.com/simple_captcha/

     

     

    posted @ 2009-08-27 20:30  麦飞  阅读(1475)  评论(0编辑  收藏  举报