Created
August 27, 2011 04:49
-
-
Save mikel/1174997 to your computer and use it in GitHub Desktop.
Simple Question Based Captcha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= f.hidden_field :captcha_question %> | |
<%= f.label :captcha_answer, 'Are you human?' %> | |
<%= f.text_field :captcha_answer %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really simple captcha implementation for Active Record models, this has been implemented as a gem, raptcha