Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails CarrierWave store image from url gives Can't convert nil into string

Using CarrierWave and Amazon S3, I'm able to store images from local files, but when I try to store them from certain url, I get 'TypeError: can't convert nil into 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>'

This is the code I'm trying

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

obviously @object holds the Uploader image and it's been working fine using local file systems. I tried to add a

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

to my uploader as well with no luck.

Any idea?

like image 583
Bashar Abdullah Avatar asked Jan 16 '23 10:01

Bashar Abdullah


1 Answers

To tell carrier wave to download a remote url, you use this syntax:

@object.remote_image_url = "http://www.example.com/file.png"
@object.save
like image 65
Jesse Wolgamott Avatar answered Jan 18 '23 22:01

Jesse Wolgamott