Uploading a remote file url from Rails Console with Carrierwave Uploading a remote file url from Rails Console with Carrierwave ruby-on-rails ruby-on-rails

Uploading a remote file url from Rails Console with Carrierwave


Take a look at the 'Uploading files from a remote location' section on this page https://github.com/carrierwaveuploader/carrierwave

CarrierWave should throw an error if the url of the location is invalid

2.1.3 :015 > image.remote_image_url = "http" => "http"2.1.3 :016 > image.save!   (0.2ms)  BEGIN   (0.2ms)  ROLLBACKActiveRecord::RecordInvalid: Validation failed: Image trying to download a file which is not served over HTTP

Or if it's an unknown host:

2.1.3 :017 > image.remote_image_url = "http://foobar"=> "http://foobar"2.1.3 :018 > image.save!   (0.4ms)  BEGIN   (0.4ms)  ROLLBACKActiveRecord::RecordInvalid: Validation failed: Image could not download file: getaddrinfo: nodename nor servname provided, or not known

Please also note that if you want to download remote images you should prefix the attribute with remote_ and suffix it with _url, as shown in the example


user = User.firstuser.remote_avatar = File.open(FILE_LOCATION)user.save

FILE_LOCATION can be

File.join(Rails.root, '/files/png-sample.png')

if file is found in a folder 'files' in rails project


I was facing the same problem. and the issue might be http is redirecting to https. So I replaced them using gsub as follows:

image.remote_image_url = remote_image_url.gsub('http://','https://')image.save!

this should most probably solve the problem.