日历

2009 11.7 Sat
1234567
891011121314
15161718192021
22232425262728
2930     
«» 2009 - 11 «»

文章搜索

日志文章

2008年07月30日 09:40:00

rails插件之Simple Captcha的使用

Simple Captcha插件获取:svn://rubyforge.org/var/svn/expressica/plugins/simple_captcha
下载后把simple_captcha放入/vendor/plugins目录



Simple Captcha插件安装:
step 1.
            当rails 版本>2.0
            rake simple_captcha:setup
            当rails 版本<2.0
rake simple_captcha:setup_old


step 2.
            rake db:migrate



step 3.
           在routs中添加


   ActionController::Routing::Routes.draw do |map|  

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

   end




step 4.
          在application.rb中添加

        ApplicationController < ActionController::Base     

            include SimpleCaptcha::ControllerHelpers 

        end
Usage
Controller Based
In the view file within the form tags add this code
[list=1]<%= show_simple_captcha %>  <%= show_simple_captcha %>[/pre]and in the controller’s action authenticate it as
[list=1]def my_action  

  •   if simple_captcha_valid?  
  •     do_this  
  •   else
  •     do_that  
  •   end
  • end def my_action if simple_captcha_valid?   do_this else   do_that endend[/pre]Model Based
    In the view file within the form tags write this code
    [list=1]<%= show_simple_captcha(:object=>"user") %>  <%= show_simple_captcha(:object=>"user") %>[/pre]and in the model class add this code
    [list=1]class User < ActiveRecord::Basse  
  •   apply_simple_captcha  
  • end class User < ActiveRecord::Basse apply_simple_captchaend[/pre]Validating with captcha
    [list=1]@user.valid_with_captcha?  @user.valid_with_captcha?[/pre]NOTE: @user.valid? will still work as it should, it will not validate the captcha code.
    Saving with captcha
    [list=1]@user.save_with_captcha  @user.save_with_captcha[/pre]NOTE: @user.save will still work as it should, it will not validate the captcha code.
    参考网址:http://expressica.com/simple_captcha/

    Tags: 验证码   rails   ruby   simple   Captcha  

    类别: ruby 经验 |  评论(0) |  浏览(998) |  收藏
    发表评论
    看不清楚,换一张