Hi friends,
Few days back I was facing difficulty in integrating recaptcha with rails so once after integrated it and I though it would be helpful for you guyz, if I mention it step-by-step as I did.
1. Open your Gemfile and add this code:
gem "recaptcha", require: "recaptcha/rails"
2. Now run bundle
bundle install
3. Then I inherited devise registration controller and overridden the create method of it.
class User::RegistrationsController < Devise::RegistrationsController
#
# Other Codes
def create
## To build the resource
build_resource(sign_up_params)
## Verifying Captcha
if verify_recaptcha(model: resource)
super
else
render 'new'
end
end
#
# Other Codes
end
4. Now you need to get the public and secret key by registering into Google recaptcha account.
The link is https://www.google.com/recaptcha/admin
5. Now add the configurations for recaptcha at /config/initializer/recaptcha.rb
Recaptcha.configure do |config|
config.public_key = 'Replace with your public key'
config.private_key = 'Replace with private key'
end
6. Add the recaptcha tags to app/views/devise/registrations/new
<div><%= recaptcha_tags %></div>
7. Now we need to tell the route to go to our registration controller instead of devise registration controller. So routes needs to be modified to:
devise_for :users, :controllers => {:registrations => "user/registrations"}
Thus you are now ready to use recaptcha with devise in your rails application.
0 Comment(s)