1

I'm working on a Rails 5.0.3 app (with Ruby 2.4.1) and am trying to install a Recaptcha v2 feature on the contact us form on my site.

I am getting the following error:-

/config/initializers/recaptcha.rb:1:in `<top (required)>': uninitialized constant Recaptcha (NameError)
    from /home/[$user]/.rvm/gems/ruby-2.4.1/gems/railties-5.0.3/lib/rails/engine.rb:648:in `block in load_config_initializer'

To implement the feature I followed the documentation for the recaptcha gem here https://github.com/ambethia/recaptcha

In my gemfile I have:-

gem 'dotenv-rails', require: 'dotenv/rails-now'
gem 'recaptcha', require: 'recaptcha/rails'

In my .env file (which is in the root folder) I have this:-

RECAPTCHA_SITE_KEY= xxxxxxxxxxxxxxxxxxxxxxxxxx
RECAPTCHA_SECRET_KEY= xxxxxxxxxxxxxxxxxxxxxxxxx

And I have this in the config/initializers/recaptcha.rb:-

Recaptcha.configure do |config|
  config.site_key  = ENV['RECAPTCHA_SITE_KEY']
  config.secret_key = ENV['RECAPTCHA_SECRET_KEY']
end

In the view I have this:-

    .
    .
    .
    <%= f.label :content %>
    <%= f.text_area :content, class: 'form-control' %>
    <br>
    <%= recaptcha_tags %>
    <br>
    <div class="actions">
      <%= f.submit "Send", class: "btn btn-primary center-block" %>
    </div>
  <% end %>
  .
  .
  .

In my controller I have this:-

  def create
    @message = Message.new(message_params)
    if !verify_recaptcha(model: @message) && @message.valid?
    .
    .
    .

As I can see from the error, the initializer isn't loading. I am not an expert on initializers so I have no clue as to how they are loaded or what I need to do to get them to load.

Ragav Y
  • 1,662
  • 1
  • 18
  • 32
  • did you do bundle install? – Sajin Jun 14 '17 at 10:03
  • several times. I also tried restarting the server. – Ragav Y Jun 14 '17 at 10:03
  • try require 'recaptcha' in initializer file – Sajin Jun 14 '17 at 10:10
  • I somehow got it to work without needing that 'require 'recaptcha'' line. There was something wrong with my repository because I had the folder in Dropbox. I did a fresh pull from git in a new folder outside of dropbox and now this error is gone and I'm able to start rails server. But I have a new error while trying to push to heroku. I started a separate question here - https://stackoverflow.com/questions/44546377/nameerror-uninitialized-constant-dotenv-while-pushing-rails-app-to-heroku/44546438#44546438 – Ragav Y Jun 14 '17 at 13:48
  • If Google takes you to this page for the same error after Jun 2019, note the renamed classes that happened in recaptcha v5.0.0 release: https://github.com/ambethia/recaptcha/blob/master/CHANGELOG.md – StuMorAthn Aug 15 '19 at 13:51

1 Answers1

3

But you dont need a gem. You render the recaptcha like this 1) import the Google script

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

2) put a div with the proper class inside the form you are interest like this

<form action="?" method="POST">
      <div class="g-recaptcha" data-sitekey="
<%=Rails.secrets.recaptcha_key%>"></div>
      <br/>
      <input type="submit" value="Submit">
</form>

Then to validate in your controller, just make a post request

if valid_captcha?(params['g-recaptcha-response']) && @user.save

where

def valid_captcha?(recaptcha_response)
    return true if Rails.env.test?

    HTTParty.post(
      Rails.application.secrets.recaptcha_url,
      body: {
        secret: Rails.application.secrets.recaptcha_secret_key,
       response: recaptcha_response
    })["success"]
end
radha
  • 458
  • 5
  • 9
  • Thanks for this, I was actually trying to implement this on my own but didn't know how to write the code for the controller method that verifies the captcha. That's why I went for the gem. Your instructions were on point. I had to do a bit of refactoring to display flash message upon failed captcha, but otherwise this was great. Thanks! – Ragav Y Jun 15 '17 at 09:43
  • @radha uninitialized constant ContactsController::HTTParty – nourza Aug 27 '18 at 21:43
  • @nourza make sure you have installed the gem https://github.com/jnunemaker/httparty – radha Feb 22 '19 at 14:53