【问题标题】:Can't upload image using Paperclip 4.0 Rails 3无法使用 Paperclip 4.0 Rails 3 上传图片
【发布时间】:2014-02-01 19:59:52
【问题描述】:

我已经安装了 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 中选择一个文件,但是一旦我单击“创建照片”按钮,页面就会重新加载,并显示回形针特定的错误消息:

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

有人可以帮忙吗? 谢谢

【问题讨论】:

    标签: ruby-on-rails windows imagemagick paperclip


    【解决方案1】:

    将此添加到初始化程序以禁用欺骗保护:

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

    【讨论】:

      【解决方案2】:

      该消息是由内容欺骗的验证检查引发的。

      对于 Paperclip v.4,这会产生一个错误 https://github.com/thoughtbot/paperclip/issues/1429

      虽然对于 Paperclip v.3,它似乎只是抛出了一个弃用警告,https://github.com/thoughtbot/paperclip/issues/1423

      所以在使用第 4 版之前,我会等待 Paperclip 团队解决此错误。目前我宁愿继续使用第 3 版。

      gem "paperclip", "~> 3.5.3"
      

      【讨论】:

      • 谢谢 - 我切换到版本 3.5.3 并且它工作。我想使用最新版本并不总是一件好事!
      • @mgambella 如果您使用的是 aws-sdk,您安装了哪个版本?
      【解决方案3】:

      这适用于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
      

      【讨论】:

        猜你喜欢
        • 2013-12-29
        • 2015-03-10
        • 2014-10-20
        • 2014-11-10
        • 2019-08-22
        • 2018-03-26
        • 1970-01-01
        • 2014-07-17
        • 1970-01-01
        相关资源
        最近更新 更多