Invisible Captcha

Episode #10 by Teacher's Avatar David Kimura

Summary

Part of a good User Experience is making your application easy to use for your users. However, a few bad apples often cause a hinderance to your UX/UI by the addition of Captchas. Learn how to create invisible captchas to protect your forms while delivering a good UX.
rails form security 4:47

Resources

Summary


Update  

In a recent version of this gem, the model helpers have since been deprecated and removed. Instead of calling the validator within your model, you can now call them in your controller on a per action basis.

For example, if you have a subject  honeypot, you can add this to your controller if you are only wanting to capture it for the create action.

invisible_captcha only: :create, honeypot: :subject, on_spam: :spam_received

private

def spam_received
  redirect_to root_path
end

# Gemfile
gem 'invisible_captcha'

# _form.html.erb
    <%= invisible_captcha %>
    <%= f.invisible_captcha :subtitle %>

# Controller
  invisible_captcha only: :send_contact, on_spam: :spam_detected
  private

  def spam_detected
    redirect_to root_path, alert: 'Spam detected'
  end

# config/initializers/captcha.rb
  InvisibleCaptcha.setup do |config|
    config.sentence_for_humans = 'If you are a human, ignore this field'
    config.error_message       = 'You are a robot!'
    config.honeypots          += 'fake_resource_title'
    config.visual_honeypots    = false
  end