简体   繁体   English

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

[英]Can't upload image using Paperclip 4.0 Rails 3

I've installed ImageMagick and I've installed the gem Paperclip (version 4.0). 我安装了ImageMagick,我已经安装了宝石Paperclip(4.0版)。 I've added: 我已经添加:

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

to the development.rb 到了开发.rb

My photo.rb Model has this: 我的photo.rb模型有这个:

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

I can choose a file in photos/new.html.erb but once I click on 'Create photo' button, the page reloads with a Paperclip specific error message saying: 我可以在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

Can someone help please? 有人可以帮忙吗? Thanks 谢谢

Add this to an initializer to disable spoofing protection: 将其添加到初始化程序以禁用欺骗保护:

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

That message is raised by a validation check for content spoofing. 该消息是通过内容欺骗的验证检查引发的。

For Paperclip v.4 this generates a bug https://github.com/thoughtbot/paperclip/issues/1429 对于Paperclip v.4,这会生成一个错误https://github.com/thoughtbot/paperclip/issues/1429

While for Paperclip v.3, it seems it just throws a deprecation warning, https://github.com/thoughtbot/paperclip/issues/1423 对于Paperclip v.3,它似乎只是抛出一个弃用警告, https://github.com/thoughtbot/paperclip/issues/1423

So I'd wait for Paperclip team to solve this bug before using version 4. At the moment I'd rather keep using version 3. 因此,在使用版本4之前,我等待Paperclip团队解决此错误。目前我宁愿继续使用版本3。

gem "paperclip", "~> 3.5.3"

This works on Paperclip v3.5.1 (hopefully will still work in V4 ): 这适用于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM