Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to use Google Recaptcha with Devise in rails application?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.18k
    Comment on it

    In web applications, Captcha is used to prevent spam/bot from getting into our applications, inbox or databases. It ensures that web application using by a real human. Here, we are going to implement Googles reCAPTCHA service in our rails application by using recaptcha gem.


    Step1: Add captcha gem into Gemfile

    gem "recaptcha", require: "recaptcha/rails"


    Step2: Run bundler

    bundle install


    Step3: Overridden the create method of devise registration controller

    class User::RegistrationsController < Devise::RegistrationsController
           def create
            #build the resource
            #Verifying Captcha
            if verify_recaptcha(model: resource) 
              render 'new'
          # Other line of Codes


     Step4: To implement Googles reCAPTCHA we need a public and private key by registering on Google recaptcha account(URL: "")


    Step5:  Now configure recaptcha like below...

    Path:   config/initializer/recaptcha.rb

    Recaptcha.configure do |config|
       config.public_key  = 'your public key'
       config.private_key = 'private key'


    Step6: Modify the routes.rb like below...

    Path: /config/routes.rb

    devise_for :users, :controllers => {:registrations => "user/registrations"}


    Step7: Display captcha in registration form

    Path: app/views/devise/registrations/new

    <%= recaptcha_tags %>


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: