现在的位置: 首页 > 综合 > 正文

在Rails里使用ReCaptcha添加验证码

2018年04月19日 ⁄ 综合 ⁄ 共 906字 ⁄ 字号 评论关闭
1,去http://recaptcha.net/sign up,获得pub key和priv key
2,安装recaptcha gem
Java代码  收藏代码
  1. gem install --source http://www.loonsoft.com/recaptcha/pkg/ recaptcha  

3,在environment.rb里设置key

Java代码  收藏代码
  1. require 'recaptcha'  
  2. RCC_PUB = 'pub key'  
  3. RCC_PRIV = 'priv key'  

4,修改application.rb

Java代码  收藏代码
  1. class ApplicationController < ActionController::Base  
  2.   include ReCaptcha::AppHelper  

5,修改application_helper.rb

Java代码  收藏代码
  1. module ApplicationHelper  
  2.   include ReCaptcha::ViewHelper  

6,在页面上显示ReCaptcha验证码

Java代码  收藏代码
  1. <%= get_captcha %>  

7,在Controller里验证验证码

Java代码  收藏代码
  1. if validate_recap(params, @comment.errors) && @comment.save  
  2.   flash[:notice] = 'Comment was successfully created.'  
  3.   format.html { redirect_to post_path(@comment.post.url_slug) }  
  4.   format.xml  { render :xml => @comment, :status => :created, :location => @comment }  

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

最终的样子:

抱歉!评论已关闭.