Recaptcha从RoR用户表单获取连接被拒绝

时间:2013-11-21 21:48:11

标签: ruby security ruby-on-rails-3.2 recaptcha

我正在尝试将reCaptcha添加到我的用户创建表单中。但我遇到了一些错误。

我在日志中看到的错误

 Nov 22 11:11:28 miningmonitor app/web.2: Recaptcha::RecaptchaError (Connection refused - connect(2))
 Nov 22 11:11:28 miningmonitor app/web.2:   app/controllers/users_controller.rb:27:in block in create
 Nov 22 11:11:28 miningmonitor app/web.2:   app/controllers/users_controller.rb:26:in create

这是我的表格代码

     <%= form_for(@user) do |u| %>
        <%= render 'shared/user_error_msg' %>

        <%= u.label :name %>
        <%= u.text_field :name %>

        <%= u.label :email %>
        <%= u.text_field :email %>

        <%= u.label :coins, "Number of coins in your wallet(ex 4.5)" %>
        <%= u.text_field :coins %>

        <%= u.label :password %>
        <%= u.password_field :password %>

        <%= u.label :password_confirmation, "Confirmation" %>
        <%= u.password_field :password_confirmation %>

        <%= recaptcha_tags %>

        <%= u.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>

现在是用于创建

的用户控制器代码
    def create
    @user= User.new(params[:user])
    @user.name.strip!
    respond_to do |format|
     if(verify_recaptcha(:model => @user))
       if @user.save
          sign_in(@user)
          format.html { redirect_to(@user, flash[:success] = "Welcome to Miners Canary!") }
       else
          format.html { render 'new' }
       end
     else
          flash.delete(:recaptcha_error)
          format.html { redirect_to(root_path, flash[:error] = "Please retry the reCaptcha Verification") }
     end
    end
end

在我的config / initializers / recaptcha.rb

Recaptcha.configure do |config|
  config.public_key = 'publickey'
  config.private_key = 'privatekey'
  config.use_ssl_by_default
  config.proxy= 'https://miningmonitor.herokuapp.com:8080'
end

我实际上在屏幕上看到了reCaptcha框。但是当我尝试注册时,我得到了上面的错误。在我的配置文件中,我将网址作为我的基本网址,而不是注册页面的确切网址。这可能是个问题吗?也可能是错误地设置了reCaptcha。我试图在github上关注这个ambethia / recaptcha指南。我希望他有更多的例子。 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

我的代码工作正在执行以下步骤,我的用户控制器看起来像这样

    def create
    @user= User.new(params[:user])
    @user.name.strip!
    if(verify_recaptcha(model: @user, message: "Error with reCaptcha!", private_key: ENV['RECAPTCHA_PRIVATE_KEY'], timeout: 10) && @user.save)
          sign_in(@user)
          flash[:success] = "Welcome to Miners Canary"
          redirect_to @user
    else
          flash.delete(:recaptcha_error)
          render 'new'
    end
end

我的用户表单如此

        <%= form_for(@user) do |u| %>
        <%= render 'shared/user_error_msg' %>

        <%= u.label :name %>
        <%= u.text_field :name %>

        <%= u.label :email %>
        <%= u.text_field :email %>

        <%= u.label :password %>
        <%= u.password_field :password %>

        <%= u.label :password_confirmation, "Confirmation" %>
        <%= u.password_field :password_confirmation %>

        <%= recaptcha_tags display: { ssl: true, theme: 'clean' , tabindex: 1, public_key: ENV['RECAPTCHA_PUBLIC_KEY'] }%>

        <%= u.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>

然后为了让它与heroku一起工作我做了这两个命令

heroku config:set RECAPTCHA_PUBLIC_KEY = 'xxxxcxxxxxxxx'

heroku config:set RECAPTCHA_PRIVATE_KEY = 'xxxxcxxxxxxxx'