无法使用Paperclip 4.0 Rails 3上传图片

5

我已经安装了ImageMagick和gem Paperclip(版本4.0)。我已经添加了:

Paperclip.options[:command_path] = 'C:\Program Files\ImageMagick-6.8.8-Q16'

到 development.rb 文件

我的 photo.rb 模型有以下内容:

has_attached_file :image
validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png', 'image/jpg']

我可以在photos/new.html.erb中选择一个文件,但是一旦我点击“创建照片”按钮,页面会重新加载,并显示一个特定于Paperclip的错误消息:

1 error prohibited this photo from being saved:
Image translation missing: 
en.activerecord.errors.models.photo.attributes.image.spoofed_media_type

可以有人帮忙吗? 谢谢

3个回答

10

将以下内容添加到初始化器中以禁用欺骗保护:

require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

4

谢谢 - 我切换到了3.5.3版本,现在可以工作了。我想使用最新版本并不总是好事! - spongey
@mgambella 如果你使用的是 aws-sdk,你安装了哪个版本? - albertopriore

0

这适用于 Paperclip v3.5.1 (希望它仍然适用于 V4):

has_attached_file :attachment,
        styles: lambda { |a| a.instance.is_image? ? { *** image_styles ***}  : { *** video_styles ***},
        processors: lambda { |a| a.is_video? ? [ :ffmpeg ] : [ :thumbnail ] }

def is_video?
    attachment.instance.attachment_content_type =~ %r(video)
end

def is_image?
    attachment.instance.attachment_content_type =~ %r(image)
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接