scientiffic
scientiffic

Reputation: 9415

Manually Setting Image Path for Carrierwave Uploader

In my rails app, I let users upload images via CarrierWave (so I have an "Image" model). I also let them embed Youtube videos (using a "Video" model). Every time the user adds a Youtube video, I'd like to create an Image record using the Youtube thumbnail image. However, when I try to create a new Image record by manually setting the path of the image, it's seen as "null" rather than the image url. For example, if enter the following into my rails console:

Image.new(:image_path=>"http://img.youtube.com/vi/O9k-MsfIkMY/default.jpg").save

I get the following log message:

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]]

How can I manually set the image path?

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

Upvotes: 2

Views: 4739

Answers (1)

Anezio Campos
Anezio Campos

Reputation: 1555

To upload remote files you need to do this https://github.com/carrierwaveuploader/carrierwave#uploading-files-from-a-remote-location

Maybe this should work:

Image.new(:remote_image_path_url=>"http://img.youtube.com/vi/O9k-MsfIkMY/default.jpg").save

Upvotes: 4

Related Questions