我正在使用具有以下型号的 Paperclip 4.2 + Rails 4.1.6:
class Post < ActiveRecord::Base
has_attached_file :featured_image, styles: { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment :featured_image, :content_type => { :content_type => ["image/jpeg", "image/gif", "image/png"] }
def featured_image_from_url(url)
self.featured_image = URI.parse(url)
end
end
当我在表单中使用文件上传器上传文件时,一切正常。设置附件并生成缩略图。
但是,如果我尝试使用指向 jpeg 图像的远程 URL,如在此处指定,则无法保存帖子,因为附件的内容类型错误:featured_image_content_type: "binary/octet-stream"
如果我通过手动设置来强制内容类型:
post.featured_image_content_type = "image/jpeg"
post.save
则模型保存成功。