2013-07-05 45 views
1

在我的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]] 

我如何可以手动设置图像路径?

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 

回答