Can't upload image using Paperclip 4.0 Rails 3

限于喜欢 提交于 2019-12-21 16:57:22

问题


I've installed ImageMagick and I've installed the gem Paperclip (version 4.0). I've added:

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

to the development.rb

My photo.rb Model has this:

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:

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


回答1:


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

While for Paperclip v.3, it seems it just throws a deprecation warning, 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.

gem "paperclip", "~> 3.5.3"



回答2:


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



回答3:


This works on Paperclip v3.5.1 (hopefully will still work in 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


来源:https://stackoverflow.com/questions/21502887/cant-upload-image-using-paperclip-4-0-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!