在我的rails应用程序中,我让用户通过CarrierWave上传图像(所以我有一个“图像”模型)。我还让他们嵌入Youtube视频(使用“视频”模型)。每次用户添加Youtube视频时,我都想使用Youtube缩略图创建图像记录。但是,当我尝试通过手动设置图像的路径来创建新的图像记录时,它被视为“空”而不是图像URL。例如,如果在我的rails控制台中输入以下内容:
Image.new(:image_path=>"http://img.youtube.com/vi/O9k-MsfIkMY/default.jpg").save
我收到以下日志消息:
INSERT INTO "images" ("caption", "created_at", "image_path", "position", "project_id", "saved", "step_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["caption", nil], ["created_at", Fri, 05 Jul 2013 15:57:54 EDT -04:00], ["image_path", nil], ["position", nil], ["project_id", nil], ["saved", nil], ["step_id", nil], ["updated_at", Fri, 05 Jul 2013 15:57:54 EDT -04:00]]
如何手动设置图像路径?
Image.rb:
class Image < ActiveRecord::Base
attr_accessible :project_id, :step_id, :image_path, :caption, :position, :saved
belongs_to :step
belongs_to :project
mount_uploader :image_path, ImagePathUploader
before_create :default_name
# validates :image_path, :presence => true
def default_name
self.image_path ||= File.basename(image_path.filename, '.*').titleize if image_path
end
end
答案 0 :(得分:4)
要上传远程文件,您需要执行此操作https://github.com/carrierwaveuploader/carrierwave#uploading-files-from-a-remote-location
也许这应该有效:
Image.new(:remote_image_path_url=>"http://img.youtube.com/vi/O9k-MsfIkMY/default.jpg").save