在我的 Rails 应用程序中,我让用户通过 CarrierWave 上传图像(所以我有一个“图像”模型)。我还让他们嵌入 Youtube 视频(使用“视频”模型)。每次用户添加 Youtube 视频时,我都想使用 Youtube 缩略图创建一个图像记录。但是,当我尝试通过手动设置图像的路径来创建新的图像记录时,它被视为“null”而不是图像 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]]
如何手动设置图像路径?
图片.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