簡體   English   中英

無法使用Paperclip 4.0 Rails 3上傳圖像

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

我安裝了ImageMagick,我已經安裝了寶石Paperclip(4.0版)。 我已經添加:

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

到了開發.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

有人可以幫忙嗎? 謝謝

將其添加到初始化程序以禁用欺騙保護:

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

該消息是通過內容欺騙的驗證檢查引發的。

對於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"

這適用於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