新手问题 gem 'simple_captcha' 无法正常安装,使用 easy_captcha 实现

ShayneChow · 2014年03月26日 · 最后由 ShayneChow 回复于 2014年03月27日 · 3718 次阅读

被这个问题折腾了很久,给回复表单加验证码功能,使用 simple_captcha。

按照https://github.com/galetahub/simple-captcha说明来操作的

gem 'simple_captcha', :git => 'git://github.com/galetahub/simple-captcha.git'
bundle update
.......
Using simple_captcha (0.1.6) from git://github.com/galetahub/simple-captcha.git (at master)
Using sorcery (0.8.5)
Using turbolinks (2.2.1)
Using uglifier (2.5.0)
Your bundle is updated!
rails g simple_captcha

rake db:migrate

感觉到这里都很正常,启动服务(使用的 RubyMine),报错:

/Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/source/git.rb:179:in `rescue in load_spec_files': git://github.com/galetahub/simple-captcha.git (at master) is not yet checked out. Run `bundle install` first. (Bundler::GitError)
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/source/git.rb:176:in `load_spec_files'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/source/path.rb:86:in `local_specs'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/source/git.rb:150:in `specs'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/lazy_specification.rb:53:in `__materialize__'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/spec_set.rb:88:in `block in materialize'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/spec_set.rb:85:in `map!'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/spec_set.rb:85:in `materialize'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/definition.rb:133:in `specs'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/definition.rb:178:in `specs_for'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/definition.rb:167:in `requested_specs'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/environment.rb:18:in `requested_specs'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/runtime.rb:13:in `setup'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler.rb:119:in `setup'
    from /Users/ShayneChow/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.5.3/lib/bundler/setup.rb:17:in `<top (required)>'
    from /Users/ShayneChow/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:135:in `require'
    from /Users/ShayneChow/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
    from /Users/ShayneChow/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:144:in `require'
    from /Users/ShayneChow/project/lexuan/config/boot.rb:4:in `<top (required)>'
    from /Users/ShayneChow/project/lexuan/bin/rails:3:in `require_relative'
    from /Users/ShayneChow/project/lexuan/bin/rails:3:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

Process finished with exit code 1

Mac OS X 10.9.2 ruby 2.0.0 rails 4.0.2 不知道是不是我的环境有问题,求解救

折腾太久这个问题,于是在 @yehualiu 的建议下选择了另一个 gem 'easy_captcha' 使用起来挺方便的。 不过还是有个问题,就是 rails s 后,报了很多很多警告,跟 rmagick 相关的(摘一段如下)

warning: already initialized constant Magick::PercentGeometry
warning: previous definition of PercentGeometry was here
warning: already initialized constant Magick::AspectGeometry
warning: previous definition of AspectGeometry was here
warning: already initialized constant Magick::LessGeometry
warning: previous definition of LessGeometry was here
……

不知道会不会存在风险。 高人路过,不吝赐教,在此谢过。

注释掉 gem 'simple_captcha', :git => 'git://github.com/galetahub/simple-captcha.git'后,rails s 正常

simple_captcha 没有用过

easy_captcha 用过 gem 'easy_captcha' gem 'rmagick'

#2 楼 @yehualiu 使用了 easy_captcha,除了修改 config/initializers/easy_captcha.rb,还需要做什么配置么?

#2 楼 @yehualiu 用 easy_captcha 整出来了,但是有个问题,目前只能整页刷新才能换验证码,怎么实现点击换一张验证码而不需要整页刷新,求解

看 imax.im 的源码哈,js 代码

#5 楼 @yehualiu 搜了一下,用一段 js 实现了动态刷新

<script>
$(document).ready(function(){
    $("img[alt='captcha']").each(function(index, item){
        item.title = '看不清?点击刷新'
    })
    $("img[alt='captcha']").bind("click", function(){
        this.src = this.src + '?' 
    })
})
</script>

挺便捷的

需要 登录 后方可回复, 如果你还没有账号请 注册新账号