Reputation: 5198
I am working with a very old rails application (v1.2.6) and need to implement a captcha on the site. Does anyone know what approach I can take to do this?
I have tried:
simple-captcha
plugin (but only works on
rails > 3)Any help would be greatly appreciated. Thanks :)
Upvotes: 0
Views: 114
Reputation: 5198
So I ended up implementing reCaptcha Non-JavaScript API as suggested by Javier.
Added the following in my view:
<p class="text-black-11px-17px" style="color: red;"><%=flash[:captcha_error]%></p>
<script type="text/javascript"
src="http://www.google.com/recaptcha/api/challenge?k=PUBLIC_KEY_HERE"></script>
<noscript>
<iframe
src="http://www.google.com/recaptcha/api/noscript?k=PUBLIC_KEY_HERE"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
Added the following in my controller:
# verify reCaptcha
uri = URI.parse("http://www.google.com/recaptcha/api/verify")
response = Net::HTTP.post_form(uri, {
"privatekey" => "PRIVATE_KEY_HERE",
"remoteip" => request.env['REMOTE_ADDR'],
"challenge" => params[:recaptcha_challenge_field],
"response" => params[:recaptcha_response_field]
})
flash[:captcha_error] = nil
if ((@contact.valid?) && (response.body.include? "success"))
# VALIDATION SUCCESSFUL - SEND THE EMAIL
elsif (response.body.include? "false")
flash[:captcha_error] = "The CAPTCHA you entered is invalid. Please try again."
render :action => 'contact'
else
render :action => 'contact'
end
Upvotes: 0
Reputation: 12536
There is no need to use a plugin, is recommendable to use when they are available, but is not a mandatory thing. Probably the "Do-it yourself" installation needs more steps but definitely will works. Take a look for example at the reCAPTCHA documentation, there is a complete section dedicated to explain the use of the library without plugins.
Basically there are two approaches:
With any of those ways, depends of your needs, hopefully you should have captcha working in a lot of scenarios without troubles. The API is well explained and have a few examples.
Hope this helps.
Upvotes: 1