ITeye博客

验证码生成插件simple_captcha

fireflyman · 2011-01-12 23:37 · ROR · 编程语言 · Markdown

插件地址:https://github.com/eshopworks/simple_captcha

(1)入门篇: 安装插件-->

script/plugin install git://github.com/eshopworks/simple_captcha.git

执行-->

rake simple_captcha:setup

在routes.rb中添加-->

map.simple_captcha '/simple_captcha/:action', :controller => 'simple_captcha'

你还需要在application_controller.rb中加入-->

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

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

 <p>
    <%= show_simple_captcha(:object => 'event') %>
 </p>

在app/models/event.rb中添加-->

class Event < ActiveRecord::Base
  apply_simple_captcha
end

将app/controllers/events_controller.rb中的create action-->

if @event.save

改为-->

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中

apply_simple_captcha :message => "image and text were different", :add_to_base => true

在对应的new.html.erb里

<p>验证码</p>
<%= show_simple_captcha(:object => 'user', :code_type => 'numeric', :image_style => 'all_black', :distortion => 'low',:label => "(请正确输入图片中的数字?)") %>

因此它看起来应该是这样的-->

(3)实操篇: 从上图可以看到,这种样式其实是很丑的.而且也没有刷新验证码的按钮.反正就是不顺眼.废话不说了,看看netfork兄的文章吧. 验证码生成换成simple_captcha了:http://netfork.iteye.com/blog/434099

以上东东只供记录.

评论(1)

  1. #1 楼 · luopeng_sg · 2011-12-28 18:01

    请问LZ这用的是JRuby还是CRuby呢?

评论功能维护中,当前仅展示历史评论。