Module: SimpleCaptcha::ControllerHelpers::InstanceMethods

Defined in:
lib/simple_captcha/controller.rb

Instance Method Summary collapse

Instance Method Details

#generate_simple_captcha_options(*args) ⇒ Object

生成验证的信息 options:

- object


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

def generate_simple_captcha_options *args
  options = args.extract_options!

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

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

end

#simple_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_simple_captcha.

Example

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

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

Returns:

  • (Boolean)


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

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

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