使用Carrierwave从Rails控制台上传远程文件URL [英] Uploading a remote file url from Rails Console with Carrierwave

查看:95
本文介绍了使用Carrierwave从Rails控制台上传远程文件URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何在Rails控制台中使用Carrierwave上传远程文件url。

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

非常感谢

推荐答案

查看此页面上的 从远程位置上传文件部分 https://github.com/carrierwaveuploader/carrierwave

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

如果该位置的网址无效,则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

或者它是未知主机:

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

请注意,如果要下载远程图像,应在属性前加上 remote_ ,并在其后缀 _url ,如示例所示

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

这篇关于使用Carrierwave从Rails控制台上传远程文件URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆