Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

Uploading a remote file url from Rails Console with Carrierwave

I just wanted to know how one would go about uploading a remote file url using Carrierwave in the Rails console.

I tried the following without any luck. I presume it's not processing the Uploader?

user = User.first
user.remote_avatar_url = "http://www.image.com/file.jpg"
user.save

Many thanks

question from:https://stackoverflow.com/questions/16918588/uploading-a-remote-file-url-from-rails-console-with-carrierwave

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)  ROLLBACK
ActiveRecord::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)  ROLLBACK
ActiveRecord::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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...