在Rails里使用ReCaptcha添加验证码

1,去[url]http://recaptcha.net/[/url]sign up,获得pub key和priv key
2,安装recaptcha gem
[code]
gem install --source http://www.loonsoft.com/recaptcha/pkg/ recaptcha
[/code]
3,在environment.rb里设置key
[code]
require 'recaptcha'
RCC_PUB = 'pub key'
RCC_PRIV = 'priv key'
[/code]
4,修改application.rb
[code]
class ApplicationController < ActionController::Base
include ReCaptcha::AppHelper
[/code]
5,修改application_helper.rb
[code]
module ApplicationHelper
include ReCaptcha::ViewHelper
[/code]
6,在页面上显示ReCaptcha验证码
[code]
<%= get_captcha %>
[/code]
7,在Controller里验证验证码
[code]
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 }
[/code]
看了下recaptcha源码,对于本地访问时validate_recap始终为true,对于错误的域名也始终为true

最终的样子:
[img]http://recaptcha.net/images/captchaHomePage.gif[/img]

你可能感兴趣的:(Ruby)