Skip to content

How To: Use Recaptcha with Devise

chaimann edited this page Mar 16, 2016 · 63 revisions

To add Google's ReCaptcha to your site:

  1. Install the ReCaptcha gem
  2. Add <%= recaptcha_tags %> to your view.
  3. Include a prepend_before_action for any action you want to secure:
class RegistrationsController < Devise::RegistrationsController
  prepend_before_action :check_captcha, only: [:create] # Change this to be any actions you want to protect.

  private
    def check_captcha
      if verify_recaptcha
        true
      else
        self.resource = resource_class.new sign_up_params
        respond_with_navigational(resource) { render :new }
      end 
    end
end

Alternatively, you can modify the create action in RegistrationsController:

class RegistrationsController < Devise::RegistrationsController

  def create
    super do |resource|
      verify_recaptcha(model: resource, message: 'reCAPTCHA verification response is incorrect, please try again.')
    end
  end

end

Some of the available options for #verify_recaptcha can be found here

Clone this wiki locally