Ruby on Rails和recaptcha

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ruby on Rails和recaptcha相关的知识,希望对你有一定的参考价值。

我有一个recaptcha的问题,我按照下一个教程:

https://github.com/ambethia/recaptcha

在我的应用程序中我有下一个:

- 在文件config/initializers/recaptcha.rb

Recaptcha.configure do |config|
  config.site_key  = Rails.application.secrets.recaptcha_site_key
  config.secret_key = Rails.application.secrets.recaptcha_secret_key
  config.proxy = ‘http://www.google.com/recaptcha/api/verify’
end
  • 在文件gemfile中 。 。 。 gem 'bootstrap-sass', '~> 3.3.7' gem 'jquery-rails', '~> 4.3' gem 'devise', '~> 4.2' gem 'toastr-rails', '~> 1.0' gem 'recaptcha', require: 'recaptcha/rails' gem 'bcrypt' . . .

在文件app / views / devise / registrations / new.html.erb中

<div class="row">
  <div class="col-md-4 col-md-offset-4">

    <h2 class="text-center">Sign up</h2>
    <br/>

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
      <%= render 'shared/devisemes' %>

      <div class="form-group">
        <%= f.text_field :fullname, autofocus: true, placeholder: "Full Name", class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.password_field :password, autocomplete: "off", placeholder: "Password", class: "form-control" %>
      </div>
      <br/></br>

      <div class="actions">
        <%= f.submit "Sign up", class: "btn btn-normal btn-block" %>
      </div>
    <% end %>

    <hr/>
    <%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path, class: "btn btn-facebook btn-block" %>
  </div>
    <div class="form-group">
  <%= raw receptcha_tags %>


  </div>
</div>

日志显示下一个:

=> Booting Puma
=> Rails 5.1.4 application starting in development 
=> Run `rails server -h` for more startup options
Exiting
/Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.3.0/lib/devise/rails/routes.rb:498:in `raise_no_secret_key': Devise.secret_key was not set. Please add the following to your Devise initializer: (RuntimeError)

  config.secret_key = '00a17c1ccd9a5dea9b6c845e0e5e46fc29d4e42e82e1146dd5df3b219b05bd7b5304125eb800b7187aa200aa096d3287d8b720f1a034439015ad1f11ffef2d1b'

Please ensure you restarted your application after installing Devise or setting the key.
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.3.0/lib/devise/rails/routes.rb:226:in `devise_for'
    from /Users/dmuino/Desktop/StJames/config/routes.rb:5:in `block in <top (required)>'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:426:in `instance_exec'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:426:in `eval_block'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:408:in `draw'
    from /Users/dmuino/Desktop/StJames/config/routes.rb:1:in `<top (required)>'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `block in load'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:55:in `block in load_paths'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:55:in `each'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:55:in `load_paths'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:18:in `reload!'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:41:in `block in updater'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.4/lib/active_support/file_update_checker.rb:81:in `execute'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:42:in `updater'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:31:in `execute_if_updated'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/application/finisher.rb:128:in `block in <module:Finisher>'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `run'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/initializable.rb:59:in `block in run_initializers'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `call'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/initializable.rb:58:in `run_initializers'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/application.rb:353:in `initialize!'
    from /Users/dmuino/Desktop/StJames/config/environment.rb:5:in `<top (required)>'
    from config.ru:3:in `require_relative'
    from config.ru:3:in `block in <main>'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.3/lib/rack/builder.rb:55:in `instance_eval'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.3/lib/rack/builder.rb:55:in `initialize'
    from config.ru:in `new'
    from config.ru:in `<main>'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.3/lib/rack/builder.rb:49:in `eval'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.3/lib/rack/builder.rb:49:in `new_from_string'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.3/lib/rack/builder.rb:40:in `parse_file'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.3/lib/rack/server.rb:319:in `build_app_and_options_from_config'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.3/lib/rack/server.rb:219:in `app'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:24:in `app'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.3/lib/rack/server.rb:354:in `wrapped_app'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:80:in `log_to_stdout'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:42:in `start'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:135:in `block in perform'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
    from /Users/dmuino/Desktop/StJames/bin/rails:9:in `require'
    from /Users/dmuino/Desktop/StJames/bin/rails:9:in `<top (required)>'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
    from /Users/dmuino/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
    from /Users/dmuino/Desktop/StJames/bin/spring:15:in `<top (required)>'
    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'

但我检查它,首先它似乎很好,但它不能正常工作。并且我的应用程序工作正常,在配置recaptcha之前,在配置recaptcha之后它不起作用。我不知道是谁的问题,我认为有更多的配置,因为当你填写表格并填写recaptcha时,你需要传递一个值来检查它,这个值很好,保存表格。如果此表单错误,请不要保存表单并显示错误。

你可以帮助我吗,我是新手,我这样做是按照一个课程,现在我想安装一个recaptcha最小的安全性

答案

我在config/secrets.yml有这个

 shared:
  recaptcha_site_key: 'site_key here'
  recaptcha_secret_key: 'secret_key here'

将校正键放在那里。

另一答案

您需要在config / initializers / devise.rb中设置设计密钥

config.secret_key = 'secret key goes here' 

阅读devise了解更多信息。或者devise tutorial

以上是关于Ruby on Rails和recaptcha的主要内容,如果未能解决你的问题,请参考以下文章

如何注销 Facebook - 在 Rails 应用程序中使用代码(Ruby on Rails Omniauth)

ruby Ruby on Rails + GitHub + CircleCI 2.0的典型代码设置

第一个CRUD的制作方法(Ruby on Rails 開發秘籍 | Ruby on Rails 快速入門)

Ruby on Rails 和 jeditable (jquery)

Windows 中的 Jenkins 和 Ruby On Rails 4

Ruby on Rails:使用清理助手在代码块内允许小于号“<”