简体   繁体   中英

reCaptcha in rails

I have installed the recaptcha gem from https://github.com/ambethia/recaptcha and I have added the

<%= recaptcha_tags >

in my form before submit I have also place public and private key in environment.rb

ENV['RECAPTCHA_PUBLIC_KEY'] = 'xxxxxxxxxxxxxxxx'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'XXXXXXXXXXXXXXxx'   

And my controller is like this

def create
  if verify_recaptcha
    super
  else
    build_resource
    clean_up_passwords(resource)
    flash.now[:alert] = "There was an error with the recaptcha code below. Please re-enter the code."
    render_with_scope :new
  end
end

I also followed the below link https://github.com/plataformatec/devise/wiki/How-To:-Use-Recaptcha-with-Devise since I also use it in my registration controller but every time the verify_recaptcha returns false can anyone plz tell me what I am missing

I was successful using your code as a starting point with the following changes:

<%= recaptcha_tags >

should be:

<%= recaptcha_tags %>

which is added in your form before submit.

I set my environmental variables local by following option #3 here: http://railsapps.github.com/rails-environment-variables.html and set them on Heroku by following: https://devcenter.heroku.com/articles/config-vars

Finally in my controller:

def create
    if verify_recaptcha
       super
    else
       flash.delete(:recaptcha_error)
       flash.now[:alert] = "There was an error with the recaptcha code below. Please re-enter the code."
       render :new
    end
end

Judging by the code, if you want to use the environment variable way of configuring recaptcha, those environment variables must be set before recaptcha is loaded.

Since the gem will be required by bundler before your initialisers run, setting those environment variables in an initialiser is too late.

Why not use the

Recaptcha.configure do |config|
  config.public_key  = ...
  ...
end

approach ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM