Skip to content

Instantly share code, notes, and snippets.

@mikel
Created August 27, 2011 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikel/1174997 to your computer and use it in GitHub Desktop.
Save mikel/1174997 to your computer and use it in GitHub Desktop.
Simple Question Based Captcha
class User < ActiveRecord::Base
attr_accessor :captcha_answer
attr_writer :captcha_question
def captcha_question
@captcha_question ||= captcha_questions.to_a[rand(a.length)].first
end
def captcha_questions
{ 'What is one plus one?' => ['two', '2'],
'What number comes after 8?' => ['nine', '9'] }
end
validates :check_captcha
private
def check_captcha
captcha_questions[captcha_question].include?(captcha_answer.to_s)
end
end
<%= f.hidden_field :captcha_question %>
<%= f.label :captcha_answer, 'Are you human?' %>
<%= f.text_field :captcha_answer %>
@mikel
Copy link
Author

mikel commented Aug 27, 2011

Really simple captcha implementation for Active Record models, this has been implemented as a gem, raptcha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment