`
hideto
  • 浏览: 2648252 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

在Rails里使用ReCaptcha添加验证码

    博客分类:
  • Ruby
阅读更多
1,去http://recaptcha.net/sign up,获得pub key和priv key
2,安装recaptcha gem
gem install --source http://www.loonsoft.com/recaptcha/pkg/ recaptcha

3,在environment.rb里设置key
require 'recaptcha'
RCC_PUB = 'pub key'
RCC_PRIV = 'priv key'

4,修改application.rb
class ApplicationController < ActionController::Base
  include ReCaptcha::AppHelper

5,修改application_helper.rb
module ApplicationHelper
  include ReCaptcha::ViewHelper

6,在页面上显示ReCaptcha验证码
<%= get_captcha %>

7,在Controller里验证验证码
if validate_recap(params, @comment.errors) && @comment.save
  flash[:notice] = 'Comment was successfully created.'
  format.html { redirect_to post_path(@comment.post.url_slug) }
  format.xml  { render :xml => @comment, :status => :created, :location => @comment }

看了下recaptcha源码,对于本地访问时validate_recap始终为true,对于错误的域名也始终为true

最终的样子:
分享到:
评论
3 楼 fireDragonpzy 2012-03-01  
Routing Error

uninitialized constant ApplicationHelper::ReCaptcha
2 楼 fireDragonpzy 2012-03-01  
gem install recaptcha
1 楼 fireDragonpzy 2012-03-01  
ERROR:  Could not find a valid gem 'recaptcha' (>= 0) in any repository
ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
    Errno::ETIMEDOUT: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 - connect(2) (http://www.loonsoft.com/reca
ptcha/pkg/latest_specs.4.8.gz)

相关推荐

Global site tag (gtag.js) - Google Analytics