`

rails3 simple captcha

 
阅读更多

安装:

ruby script/plugin install git://github.com/kares/simple_captcha.git

 安装之后,跟随下面的步骤配置插件:

#### STEP 1

rails generate simple_captcha

#### STEP 2

rake db:migrate

#### STEP 3
   将下面的代码添加到 config/routes.rb :
 

 Keditor::Application.routes.draw do
      match '/simple_captcha/:action', :controller => 'simple_captcha', :as => :simple_captcha
 end

 
#### STEP 4
 配置simple_captcha e.g. in `app/config/initializers/simple_captcha.rb`

前提条件是系统安装了rmagick和相关的gem 安装rmagick
 

SimpleCaptcha.backend =  :RMagick
    SimpleCaptcha.image_options = {
        :image_color => 'white',
        :image_size => '110x30',
        :text_color => 'black',
        :text_font => 'arial',
        :text_size => 22
    } # these are the defaults

 
 添加代码到app/controllers/application.rb 

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

 

添加代码到model,以user为例子,注册时要求验证码

 

class User < ActiveRecord::Base
      include SimpleCaptcha::ModelValidation
      validates_captcha :on => :create, :message => 'invalid captcha'
end
 

 

singup.html.erb注册页面加入show_simple_captcha

 

<%=form_for @user,:as=>:user, :url=>users_path, :method=>"put",:html=>{:name=>"regform",:id=>"regform"} do%>
 <li style="display:block;" class="captchadiv">
	<div class="reg_formlabel">验 证 码:</div>
	<div class="reg_element ">
	  <span class="inp_box">
	    <div class="field" id="div_simple_captcha">
<%= show_simple_captcha :object=>"user",:label=>"请输入图片中的文字",:distortion=>:high%></div>
	  </span>
	  <%if @user.errors.on(:captcha)%>
		<div style="display:block" class="reg_tips form-valid-tip-err">
			<div style="display:block;" class="error l">
			  <span class="form-valid-err-content"><%=@user.errors.on(:captcha)%></span>
			</div>
		</div>
		<%end%>
	</div>
</li>
<%end%>
 

 

默认插件不支持切换验证码,需手动添加   

 

config/routes.rb :

resources :users do 
   collection do
       get 'update_captcha'
    end
end

  users  controller:

def update_captcha
    respond_to do |format|
      format.js
    end		
end

 users view:update_captcha.rjs

page.replace_html "div_simple_captcha",show_simple_captcha(:object=>"user",:label=>"请输入图片中的文字",:distortion=>:high)

 

修改插件view的代码,加入一个链接:#{Rails.root.to_s}/vendor/plugins/simple_captcha/app/views/simple_captcha/_simple_captcha,前提要载入jquery

<div id='simple_captcha'>
  <div class='simple_captcha_image'>
    <%= @simple_captcha_options[:image] %>
  </div>
  
  <div class='simple_captcha_field'>
    <%= @simple_captcha_options[:field] %>
  </div>
  
  <div class='simple_captcha_label'>
    <%= @simple_captcha_options[:label] %>
    <a onclick="jQuery.ajax({data:'', dataType:'script', type:'post', url:'/users/update_captcha'}); return false;" href="#">换一张</a>
  </div>
</div>

 

 

 

分享到:
评论

相关推荐

    rails 3 upgrade handbook(simple樣例)

    A step-by-step guide to upgrading your app to Rails 3 High-level discussion of what's new in Rails 3 Practical tips on using Rails 3's new features to improve your code Real case studies of upgrading ...

    simple-captcha

    SimpleCaptcha2可以与Rails 3 + 4一起使用。这是流行的Rubygem simple_captcha一个分支,被遗弃了。 ##特征 零FileSystem使用率(将秘密代码移至db-store并删除了图像存储)。 提供各种图像样式。 提供三个...

    rails3教程

    Rails3教程系列 DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY...

    RVM_Ruby1.9.3_Rails3(2-Ruby on Rails3安装配置)

    Windows7_Cygwin_Git_RVM_Ruby1.9.3_Rails3_MongoD B_Nginx_Unicorn_Rspec_Guard_Spork(2-Ruby on Rails3 安装配置

    Ruby-SimpleForm轻松处理Rails表单

    Simple Form - 轻松处理Rails表单

    turbo-sprockets-rails3, 加速你的Rails 3资产.zip

    turbo-sprockets-rails3, 加速你的Rails 3资产 用于 Rails 3.2.x的涡轮链轮 通过只根据源文件的哈希来重新编译已经更改的资产,从而加快 Rails 3 rake assets:precompile的速度只编译一次以生成指纹和非打印的资产...

    Rails 3 in Action

    Rails 3 in Action 2011年9月新鲜出炉,针对最新的Rails 3.1进行说明

    The Rails 3 Way(2nd)

    The Rails™ 3 Way is a comprehensive resource that digs into the new features in Rails 3 and perhaps more importantly, the rationale behind them. —Yehuda Katz, Rails Core The Bible for Ruby on ...

    simple-captcha2

    SimpleCaptcha2 可以与 Rails 3 + 4 一起使用。这是流行的 Rubygem simple_captcha一个分支,它被废弃了。 ##特征 零文件系统使用(秘密代码移至 db-store 并删除图像存储)。 提供各种图像样式。 提供三个...

    基于java的开发源码-Rails3消息队列系统 Sidekiq.zip

    基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip ...

    The Rails3 Way, 2nd Edition

    The Rails™ 3 Way is the only comprehensive, authoritative guide to delivering production-quality code with Rails 3. Pioneering Rails expert Obie Fernandez and a team of leading experts illuminate ...

    Rails3消息队列系统 Sidekiq

    Rails3消息队列系统 Sidekiq

    Rails3的ActiveRecord 查询API.doc

    Rails3的ActiveRecord 查询API.doc

    rails金典教材3本

    Ruby+on+Rails+3+Tutorial.pdf 应用Rails进行敏捷Web开发第4版.pdf (Agile Web Development with Rails) Rails.Recipes.Rails.3.Edition.pdf

    centOS Rails3环境搭建

    3 rails # gem install rails -v=2.3.11 4 passenger # gem install passenger passenger-install-apache2-module 安装完成后按照提示将下面的内容添加到etc/httpd/httpd.conf LoadModule pa

    rails3文档

    rails3的英文文档

    Rails3常用命令行命令

    rails常用命令,例如新建rails项目,新建controller、model 等等

    ruby on rails 3 tutorial.pdf

    ruby on rails开发的初学者很适合的工具书,易上手,很实用,

    [Rails] Crafting Rails Applications (英文版)

    This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more ...

Global site tag (gtag.js) - Google Analytics