Google Recaptcha With Ruby On Rails Integration.
Google Recaptcha protects the websites you love from spam and abuse.
Below the steps to be followed to develop a sample ROR application with Google Recaptcha:-
Step 1. Create a Ruby on Rails application:-
a)Open a terminal, navigate to a directory where you have rights to create application and type:
rails new recap
b)After you create the application, switch to its folder:
cd recap
c)Type and run bundle install:
bundle install
Step 2. Create models, views, and controllers:-
a)For this application we will generate a scaffold, which is a starter template provided by Rails that bundles a model, a controller, and the relevant views for our application:
rails generate scaffold User name:string
b)After generating the scaffold we need to create the actual User table in our development database so we need to run migration:
rake db:migrate
c)To start the application run below command and open browser and type http://localhost:3000/users
rails s
d)Configure application with nginx or apache with application url eg:gurudathcap.com
(For configuration settings please see the reference url at the botton of the post.)
Step 3. Integrating Google Recaptcha With Ruby On Rails:-
a)Please login to Google Recaptcha website to register ur domain to get access.(https://www.google.com/recaptcha/intro/index.html)
b)Please login and register your site , with the details as shown in below form.
c)After registeration google provide the
- Script tag place this snippet before the closing </head> tag on your HTML template.
- div place this snippet at the end of the <form> where you want the reCAPTCHA widget to appear
d)Once the above steps are done, we can see the recaptcha in the site.
f)For the server side validation we can use the secret key and the response which is going to be sent as parameters to the form submit action in the controller.
g)To check whether Google has verified that user, send a GET request with these parameters:URL: https://www.google.com/recaptcha/api/siteverify
Step 4. Application Code change for server side validation.
a)We will write a helper to request google and get the status:-
def verify_google_recptcha(secret_key,response)
status = `curl “https://www.google.com/recaptcha/api/siteverify? secret=#{secret_key}&response=#{response}”`
hash = JSON.parse(status)
hash[“success”] == true ? true : false
end
b)Now we can use this helper the controller to verify status:-
status = verify_google_recptcha(SECRET_KEY,params[‘g-recaptcha-response’])
where status going to be true or false. If false we will render the form page again for resubmit else we will accept the form.
OUTPUT SLIDES:-
NOTE :-
To configure application with nginx or apache reference url:-
http://nathanhoad.net/how-to-ruby-on-rails-ubuntu-apache-with-passenger
http://www.rabblemedia.net/installing-rvm-ruby-on-rails-and-passenger-on-ubuntu.html
Now the google recaptcha protects your websites from spam and abuse. We will update with more information with upcoming days.
Regards
Gurudath BN
nice
LikeLike
Wow, this article is fastidious, my younger sister is analyzing these kinds of things, thus I am going to convey her. efeefdededda
LikeLiked by 1 person
Thanks Johnc491 .
LikeLike
Reblogged this on Dinesh Ram Kali..
LikeLike
Reblogged this on Rajesh Paul.
LikeLike
Thanx for this. Will try this out definitely.
LikeLike
Nice Article. I found one more article on this topic on RailsCarma’s Blog.
It’s worth reading.
http://www.railscarma.com/blog/technical-articles/protect-your-web-application-with-google-recaptcha-on-ruby-on-rails/
LikeLiked by 2 people
Hi Gurudath, how did you configure the heroku url in the recaptcha panel?
LikeLike
Yes
LikeLike
Hello! Do you use Twitter? I’d like to follow you if that
would be okay. I’m definitely enjoying your blog and look forward to new posts.
LikeLike
I have a twitter account, but I am not so active on it. This is my twitter id @Gurudath1989
LikeLike