簡體   English   中英

軌道中的reCaptcha

[英]reCaptcha in rails

我已經從https://github.com/ambethia/recaptcha安裝了recaptcha gem,並添加了

<%= recaptcha_tags >

在提交表單之前,我還已經將public和private密鑰放在environment.rb中

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

我的控制器就是這樣

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

我也遵循以下鏈接https://github.com/plataformatec/devise/wiki/How-To:-Use-Recaptcha-with-Devise,因為我也在注冊控制器中使用了它,但是每次verify_recaptcha返回false時,任何人都可以請告訴我我所缺少的

我成功地將您的代碼用作以下更改的起點:

<%= recaptcha_tags >

應該:

<%= recaptcha_tags %>

提交之前,您的表單中已添加了該文件。

我通過以下選項#3在本地設置環境變量: http : //railsapps.github.com/rails-environment-variables.html並通過以下操作在Heroku上進行設置: https : //devcenter.heroku.com/articles/配置變量

最后在我的控制器中:

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

從代碼來看,如果要使用配置Recaptcha的環境變量方式,則必須在裝入Recaptcha之前設置這些環境變量。

由於捆綁程序需要在啟動初始化程序之前運行gem,因此在初始化程序中設置這些環境變量為時已晚。

為什么不使用

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

方法?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM