【问题标题】:How can save image using only path in Paperclip mongoid如何仅使用 Paperclip mongoid 中的路径保存图像
【发布时间】:2017-09-24 00:39:34
【问题描述】:

在我的项目中,我没有使用表单来发送图像。我正在从路径中获取。

如何仅使用路径保存图像?

我有这个模型

class Picture
  include Mongoid::Document
  include Mongoid::Paperclip

  has_mongoid_attached_file :avatar
end

我试过这个(在回形针和 mysql 中这个工作)

p = Picture.new
p.avatar = File.open('/path/to/my/image.jpg')
p.save

如果我这样做,我会收到错误。

Paperclip::Errors::MissingRequiredValidatorError: Paperclip::Errors::MissingRequiredValidatorError

我该怎么做?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby mongodb mongoid paperclip


    【解决方案1】:

    我忘记了验证:

    validates_attachment_content_type :avatar,
       :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
    

    这将解决问题。

    【讨论】:

      猜你喜欢
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      相关资源
      最近更新 更多