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.22k
    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
            build_resource(sign_up_params)   
            #Verifying Captcha
            if verify_recaptcha(model: resource) 
              super
            else
              render 'new'
            end
          end
          #
          # Other line of Codes
        end

     

     Step4: To implement Googles reCAPTCHA we need a public and private key by registering on Google recaptcha account(URL: "https://www.google.com/recaptcha/admin")

     

    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'
    end

     

    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
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: