huacnlee / rucaptcha

Captcha Gem for Rails, which generates captcha image by Rust.
https://huacnlee.github.io/rucaptcha
MIT License
693 stars 133 forks source link

使用devise,当verify_rucaptcha? 后不跟resource参数时,resource.errors里还是出现error #77

Open zll11111 opened 5 years ago

zll11111 commented 5 years ago

你好: 我使用了devise,不想将验证码失败的error放在user里,想将注册时字段错误信息和验证码失败信息分开,所以想调用verify_rucaptcha? nil,如下例子:

class Users::RegistrationsController < Devise::RegistrationsController

  def create
    build_resource(sign_up_params)
    if verify_rucaptcha?
      resource.save
      yield resource if block_given?
      if resource.persisted?
        if resource.active_for_authentication?
          set_flash_message! :notice, :signed_up
          sign_up(resource_name, resource)
          respond_with resource, location: after_sign_up_path_for(resource)
        else
          set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
          expire_data_after_sign_in!
          respond_with resource, location: after_inactive_sign_up_path_for(resource)
        end
      else
        clean_up_passwords resource
        set_minimum_password_length
        respond_with resource
      end
    else
      #resource.errors.clear
      flash.now[:alert] = t('rucaptcha.invalid')
      clean_up_passwords resource
      respond_with resource
    end
  end
end

2019-04-04 22-18-07屏幕截图

随后看了一下代码,应该是rucaptcha-2.5.0/lib/rucaptcha/controller_helpers.rb里的75行 add_rucaptcha_validation_error方法里resource引用的是全局resource,不是verify_rucaptcha?传进去的resource。