2013-07-05 82 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 

回答