Search code examples
ruby-on-railskeyrecaptchaproductionruby-on-rails-5.1

Recaptcha rails 5 error


I am having an error with recaptcha recognizing my keys in secrets.yml in production. In development, everything works fine!

I was able to upload successfully my site using capistrano and nginx, I placed the recaptcha keys in secrets.yml but I get the following error in rails logs.

[70624ace-d7c2-41d4-a312-81e558237559] app/views/contacts/_new.html.erb:1:in `_app_views_contacts__new_html_erb___113748072819217608_34958780'
[70624ace-d7c2-41d4-a312-81e558237559] app/views/welcome/index.html.erb:97:in `_app_views_welcome_index_html_erb__2483501284442413835_31069860'
I, [2018-02-13T15:06:06.655443 #23369]  INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Started GET "/" for 66.249.70.22 at 2018-02-13 15:06:06 +0000
I, [2018-02-13T15:06:06.656262 #23369]  INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Processing by WelcomeController#index as HTML
I, [2018-02-13T15:06:06.657082 #23369]  INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f]   Rendering welcome/index.html.erb within layouts/application
I, [2018-02-13T15:06:06.666156 #23369]  INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f]   Rendered contacts/_new.html.erb (8.3ms)
I, [2018-02-13T15:06:06.666316 #23369]  INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f]   Rendered welcome/index.html.erb within layouts/application (9.1ms)
I, [2018-02-13T15:06:06.666485 #23369]  INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Completed 401 Unauthorized in 10ms
F, [2018-02-13T15:06:06.667938 #23369] FATAL -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f]
F, [2018-02-13T15:06:06.668014 #23369] FATAL -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] ActionView::Template::Error (No site key specified.):
F, [2018-02-13T15:06:06.668157 #23369] FATAL -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f]      9:

I am trying to follow this tutorial https://github.com/ambethia/recaptcha, but it does not provide much detail about placing the keys in production.

I have tried many things, like adding export in front of the keys, placing them in different files, and checking secrets.yml for any syntax error. I have also made sure that I am using RECAPTHCA_SITE_KEY instead of RECAPTCHA_PUBLIC_KEY.

Also, I have placed my domain in recaptcha's site.

I would appreciate if you could help me solve the problem as well as provide some explanation of why I was having the error.

This is my secrets.yml in production:

production:
  secret_key_base: ...SECRET_KEY...
  RECAPTCHA_SITE_KEY: "...SITE_KEY..."
  RECAPTCHA_PRIVATE_KEY: "...PRIVATE_KEY..."

Thanks!


Solution

  • You need to configure recaptcha to pull the keys from your secrets.yml by adding an initializer file, config/initializers/recaptcha.rb:

    # config/initializers/recaptcha.rb
    Recaptcha.configure do |config|
      config.site_key   = Rails.application.secrets[:RECAPTCHA_SITE_KEY]
      config.secret_key = Rails.application.secrets[:RECAPTCHA_PRIVATE_KEY]
      # Uncomment the following line if you are using a proxy server:
      # config.proxy = 'http://myproxy.com.au:8080'
    end