<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hwork.org &#187; Rails</title>
	<atom:link href="http://hwork.org/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://hwork.org</link>
	<description>Henry Work Blog.  Developer for TouringPlans.com. Formerly of TechCrunch (co-built CrunchBase).  Once team captain of the Northern Bites.</description>
	<lastBuildDate>Tue, 09 Feb 2010 01:50:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>reCAPTCHA on Rails</title>
		<link>http://hwork.org/2009/03/08/recaptcha-on-rails/</link>
		<comments>http://hwork.org/2009/03/08/recaptcha-on-rails/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 04:46:57 +0000</pubDate>
		<dc:creator>Henry Work</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://hwork.org/?p=350</guid>
		<description><![CDATA[
This has probably been done to death, but for the sake of Random Google Searches (RGSs), here is a quick run through about how to do reCAPTCHA with Rails.
First off, you need to register at recaptcha.net.  Then you need to add your domain and get two keys (one public, one private).  I created [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://hwork.org/wp-content/uploads/2009/03/recaptcha_logo.png" style="float: right;" width="200" height="134" /></p>
<p>This has probably been done to death, but for the sake of Random Google Searches (RGSs), here is a quick run through about how to do <a href="http://recaptcha.net/" title="reCAPTCHA">reCAPTCHA</a> with Rails.</p>
<p>First off, you need to <a href="https://admin.recaptcha.net/accounts/signup/" title="register">register</a> at recaptcha.net.  Then you need to 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.</p>
<p>Second, <strong>there is a Rails plugin</strong>.  <a href="http://github.com/ambethia/recaptcha/tree/master" title="It's on GitHub">It&#8217;s on GitHub</a>.   So install it like this:</p>
<blockquote><p>$ ./script/plugin install git://github.com/ambethia/recaptcha.git</p></blockquote>
<p>Third, take those public and private reCAPTCHA keys and place those suckers in your environment.rb or appropriate environment file.  Here&#8217;s what I added to the bottom of my development.rb, replacing the &#8216;MY_PUBLIC_KEY&#8217; and &#8216;MY_PRIVATE_KEY&#8217; with the keys from recaptcha.net (but keep the single quotes):</p>
<blockquote><p><code><br />
ENV['RECAPTCHA_PUBLIC_KEY'] = 'MY_PUBLIC_KEY'<br />
ENV['RECAPTCHA_PRIVATE_KEY'] = 'MY_PRIVATE_KEY'<br />
</code></p></blockquote>
<p>Fourth, find the place in your views where you want the reCAPTCHA box to appear.  The plugin defines a special view helper named <code>recaptcha_tags</code>.  Here&#8217;s a basic example:</p>
<blockquote><p><code></p>
<p>Ask a Question -- Get an Answer!</p>
<p><% form_for(@question) do |f| %><br />
	Question: <%= f.text_field :question %><br />
	Human Test: <%= recaptcha_tags %><br />
	<%= f.submit "Ask Question" %><br />
<% end %><br />
</code></p></blockquote>
<p>And here&#8217;s what it that basic html looks like:</p>
<p><center><img src="http://hwork.org/wp-content/uploads/2009/03/recaptcha1.png" wdith="334" height="235" /></center></p>
<p>Each page load will embed code that pings the <a href="http://recaptcha.net/apidocs/captcha/">reCAPTCHA API</a> and generates a new captcha.  Note that there are some extra options on <code>recaptcha_tags</code> if you need to handle ssl or want to by default not use javascript (uses an iframe instead).  </p>
<p>Lastly, we have to handle the verification in the controller.  Here&#8217;s how I integrated it:</p>
<blockquote><p><code><br />
 def create<br />
    @question = Question.new(params[:question])<br />
    if verify_recaptcha() and @question.save<br />
      redirect_to :action => 'show', :permalink => @question.permalink<br />
    else<br />
      render :action => 'new'<br />
    end<br />
 end<br />
</code></p></blockquote>
<p>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&#8217;ll render the new page again, where a small error message will show up in the reCAPTCHA box.</p>
<p>And that&#8217;s about it.  Hat tip to <a href="http://thinkingdigitally.com/" title="Rob Olson">Rob Olson</a> for implementing this originally on <a href="http://pitches.techcrunch.com" title="Elevator Pitches">Elevator Pitches</a> (I checked out that implementation first).  </p>
]]></content:encoded>
			<wfw:commentRss>http://hwork.org/2009/03/08/recaptcha-on-rails/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.230 seconds -->
