【问题标题】:Manually Setting Image Path for Carrierwave Uploader手动设置 Carrierwave Uploader 的图像路径
【发布时间】:2013-07-05 19:59:23
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails carrierwave


    【解决方案1】:

    要上传远程文件,您需要这样做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
    

    【讨论】:

    • 这行得通,谢谢!只需将 :remote_image_path_url 添加到我的图像模型中
    猜你喜欢
    • 2023-03-13
    • 2011-04-26
    • 2018-10-16
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    相关资源
    最近更新 更多