Module: TinyCaptcha::ControllerHelpers::InstanceMethods

Defined in:
lib/tiny_captcha/controller.rb

Instance Method Summary collapse

Instance Method Details

#generate_tiny_captcha_options(*args) ⇒ Object

生成验证的信息options:

- object


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tiny_captcha/controller.rb', line 41

def generate_tiny_captcha_options *args
  options = args.extract_options!

  key = TinyCaptcha::Utils.generate_key_use_session(
    session, options[:object]
  )

  {
    :image_url   => ::TinyCaptcha::Utils.generate_image_url(key, request.base_url, options),
    :label       => options[:label] || I18n.t('tiny_captcha.label'),
    :captcha_key => ::TinyCaptcha::Utils.set_tiny_captcha_data(key, options),
    :object      => options[:object]
  }

end

#tiny_captcha_valid?Boolean

This method is to validate the simple captcha in controller. It means when the captcha is controller based i.e. :object has not been passed to the method show_tiny_captcha.

Example

If you want to save an object say @user only if the captcha is validated then do like this in action…

if tiny_captcha_valid?
 @user.save
else
 flash[:notice] = "captcha did not match"
 redirect_to :action => "myaction"
end


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tiny_captcha/controller.rb', line 25

def tiny_captcha_valid?
  return true if Rails.env.test?

  if params[:captcha]
    data = TinyCaptcha::Utils::tiny_captcha_value(params[:captcha_key] || session[:captcha])
    result = data == params[:captcha].delete(" ").upcase
    TinyCaptcha::Utils::tiny_captcha_passed!(session[:captcha]) if result
    return result
  else
    return false
  end
end