对于#<string:0xa042bf8>的未定义方法`to_file' - IMGKit </string:0xa042bf8>

时间:2014-01-03 01:37:29

标签: ruby-on-rails wkhtmltoimage imgkit

我正在尝试从某些网站拍摄快照,但我遇到了IMGKit的问题。

当我尝试在Rails任务上运行此代码时:

kit = IMGKit.new('http://www.adlibituradventures.com/')
kit = kit.to_img(:jpg) 
file_path = 'public/images/myimage.jpg'
file = kit.to_file(file_path)

我收到以下错误:

rake aborted!
undefined method `to_file' for #<String:0xa042bf8>
/home/paladini/Documents/Projetos/bittle/btc-stores/lib/tasks/crawl_usebitcoin.rake:269:in `get_image'
/home/paladini/Documents/Projetos/bittle/btc-stores/lib/tasks/crawl_usebitcoin.rake:113:in `block (3 levels) in <top (required)>'
/home/paladini/Documents/Projetos/bittle/btc-stores/lib/tasks/crawl_usebitcoin.rake:97:in `each'
/home/paladini/Documents/Projetos/bittle/btc-stores/lib/tasks/crawl_usebitcoin.rake:97:in `block (2 levels) in <top (required)>'
/home/paladini/Documents/Projetos/bittle/btc-stores/lib/tasks/crawl_usebitcoin.rake:43:in `block (2 levels) in <top (required)>'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@btc-stores/bin/ruby_executable_hooks:15:in `eval'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@btc-stores/bin/ruby_executable_hooks:15:in `<main>'

我的config/initializers/imgkit.rb

IMGKit.configure do |config|
  config.default_options = {
    :'use-xserver' => true
  }
end

如果删除配置文件,则执行我获得的rake任务:

rake aborted!
Command failed: /usr/local/bin/wkhtmltoimage --height 1000 --format jpg http://www.adlibituradventures.com/ -: Loading page (1/2)
QSslSocket: cannot resolve SSLv2_client_method               ] 10%
QSslSocket: cannot resolve SSLv2_server_method
No bp log location saved, using default.                     ] 36%
[000:000] Cpu: 6.37.5, x4, 2399Mhz, 2893MB
[000:000] Computer model: Not available
[000:000] Browser XEmbed support present: 1
[000:000] Browser toolkit is not Gtk2 (0).
[000:001] Using Xt toolkit
No bp log location saved, using default.
[000:000] Cpu: 6.37.5, x4, 2399Mhz, 2893MB
[000:000] Computer model: Not available
Rendering (2/2)                                                    
QPixmap: Cannot create a QPixmap when no GUI is being used   ] 25%
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::translate: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setWorldTransform: Painter not active
QPainter::setOpacity: Painter not active
QPainter::setFont: Painter not active
*** NSPlugin Viewer  *** *** NSPlugin Viewer  *** ERROR: rpc_end_sync called when not in sync!
ERROR: rpc_end_sync called when not in sync!

所以,我正在尝试使用这个配置文件,因为this tutorial告诉我--use-xserver标志解决了QPixMap的问题。但我不知道问题是IMGKit还是wkhtmltoimage。

在您提问之前,我目前正在使用:

  • Rails 4.0
  • Ruby 2.0
  • Imgkit 1.3.10

请有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

kit = kit.to_img(:jpg)

然后将kit更改为字符串。

如果要保留kit,请更改变量名称。