来自url的Rails CarrierWave存储图像无法将nil转换为字符串

时间:2012-06-11 20:15:02

标签: ruby-on-rails mongoid carrierwave

使用CarrierWave和Amazon S3,我能够存储来自本地文件的图像,但是当我尝试从某个URL存储它们时,我得到'TypeError:无法将nil转换为String'

    from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/cache.rb:149:in `join'
    from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/cache.rb:149:in `cache_path'
    from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/cache.rb:121:in `block in cache!'
    from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
    from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/cache.rb:112:in `cache!'
    from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/store.rb:56:in `store!'
    from (irb):5
    from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
    from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
    from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

这是我正在尝试的代码

require 'open-uri'
image = "open(image_url)
@object.image.store!(file)

显然@object拥有Uploader图像,它使用本地文件系统工作正常。我试着添加一个

def root     Rails.root.join'public /'   端

我的上传者也没有运气。

有什么想法吗?

1 个答案:

答案 0 :(得分:9)

要告诉载波下载远程URL,请使用以下语法:

@object.remote_image_url = "http://www.example.com/file.png"
@object.save