Sunday, February 21, 2010

reCAPTCHA on Rails [Installing Rails plugin on Windows]

1. Register at recaptcha.net. Add your domain and get two keys (one public, one private). I created two sites once registered, one for my development environment (localhost), and one for production.
2. Install recaptcha plugin for Rails. (GitHub).
$ ./script/plugin install git://github.com/ambethia/recaptcha.git
------------------------------------------------------------
This might be PITA for the Windows users. I wasted 3-4 hrs figuring this out. The links I used are :-
a. http://allaboutruby.wordpress.com/2009/07/20/installing-rails-on-windows-3-years-later/
b. http://rails.webintellix.com/index.php/2009/05/installing-rails-plugins-from-github-on-windows/
c. http://groups.google.ca/group/rubyonrails-talk/browse_thread/thread/0983f0dabbe3b9e7/6499ff5e9f80156a?#6499ff5e9f80156a
Check if these links work for you since they are relatively simple and worked for some users. But I always faced the issue :-
C:\Projects\rajdhani>ruby script/plugin install http://github.com/ambethia/recaptcha.git/
Plugin not found: [http://github.com/ambethia/recaptcha.git/]
Solution to installing Rails plugin on Windows
1. Install git for Windows : http://code.google.com/p/msysgit/downloads/list [All default options. Do not change any options]. You might want to restart your computer.
2. Start Git GUI->Clone repository->Source:git://github.com/ambethia/recaptcha.git->Destination: C:\<project_name>\vendor\plugins\recaptcha [needs to be a new folder]
3. Restart the Rails server
------------------------------------------------------------
3. Take the public and private reCAPTCHA keys and save to environment.rb or your environment file. I added the following to the bottom of my development.rb, replacing the ‘MY_PUBLIC_KEY’ and ‘MY_PRIVATE_KEY’ with the keys from recaptcha.net (include single quotes):

ENV['RECAPTCHA_PUBLIC_KEY'] = 'MY_PUBLIC_KEY'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'MY_PRIVATE_KEY'
4. Edit views where you want the reCAPTCHA box to appear. The plugin defines a special view helper named recaptcha_tags. Here’s a basic example:
<label for="project_is_sac">Is a SAC project? </label> &nbsp;&nbsp; <%=f.check_box :is_sac><br/><br/>
    Human Test: <%= recaptcha_tags %>
  <p>
    <%= f.submit 'Create' %>
  </p>
And here’s what it that basic html looks like:
image
Each page load will embed code that pings the reCAPTCHA API and generates a new captcha. Note that there are some extra options on recaptcha_tags if you need to handle ssl or want to by default not use javascript (uses an iframe instead).
5. Add verification in the controller:


if verify_recaptcha() and @project.save
        flash[:notice] = 'Project was successfully created.'
        format.html { redirect_to(@project) }
else
        format.html { render :action => "new" }
end

The verify_recaptcha() method will take params from the POST request, ping recaptcha.net, and then return true or false. Then you can handle it however you want (here in the same block as model validation). If the captcha fails, it’ll render the new page again.
image

No comments:

Post a Comment

Thank you for your feedback