Skip to content

How To: Use Recaptcha with Devise

printercu edited this page Apr 21, 2016 · 41 revisions
Clone this wiki locally

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.

    def check_captcha
      if verify_recaptcha
        self.resource = sign_up_params
        respond_with_navigational(resource) { render :new }
  1. If you are adding reCaptcha in registration page then add the below lines in config/routes.rb. The first one is configuration of devise generated controller (rails g devise:controller [scope]), but if you are using your own "registrations_controller.rb" then use it accordingly.

a) For Devise Generated Controller Route Settings (config/routes.rb):

devise_for :users, controllers: { ... , registrations: "user/registrations", ... }

b) For custom "registrations_controller.rb" in "app/controller" directory, use this Route Settings (config/routes.rb):

devise_for :users, controllers: { ... , registrations: "registrations", ... }

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

Something went wrong with that request. Please try again.