2

当我尝试上传不在["image/jpg", "image/jpeg", "image/gif", "image/png", "image/pjpeg", "image/x-png"]

当我尝试上传像“wav”这样的文件时,我收到了这条消息

* Photo /var/folders/nT/nTr21TWfFRO7r3cyG-h-7++++TM/-Tmp-/Clip audio 01,39154,0.wav is not recognized by the 'identify' command. * Photo /var/folders/nT/nTr21TWfFRO7r3cyG-h-7++++TM/-Tmp-/Clip audio 01,39154,0.wav is not recognized by the 'identify' command. * Photo content type Accepted files include: jpg, gif, png

所以它检测到文件不是图像并显示我的消息"Accepted files include: jpg, gif, png",但我在我的照片之前包含了这个额外的消息“识别”命令无法识别...上传图片可以正常工作

我的代码是:

控制器:

def upload  
  @picture= Picture.new(params[:picture])  
    if !@picture.valid?  
        render  :form  
    end  
end  

查看表格:

<%= error_messages_for :picture, :header_message => nil, :message => nil %>  
<% form_for :picture, @picture, :name => "uploadPic", :url => { :action => 'upload_data'}, :html => {:name => 'uploadForm', :multipart => true } do |form| %>  
    <%= form.file_field :photo %>  
    <%= submit_tag 'Save'%>  
<% end %>

图片型号:

 class Picture < ActiveRecord::Base    
    require 'paperclip'  
    has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }  

    validates_attachment_size :photo, :less_than => 2.megabytes , :message => "must be less than 2 megabytes"  
    validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png", "image/pjpeg", "image/x-png"], :message => "Accepted files include: jpg, gif, png"   

 end
4

2 回答 2

7

用 :whiny => false
has_attached_file :photo, :whiny => false, :styles => { :medium => "300x300>", :thumb => "100x100>" }

于 2010-01-22T01:03:09.090 回答
3

:whiny => false 不足以解决最新版本的回形针 (2.3.6) 的问题。我最终在 Rails 初始化程序中执行此操作:

module Paperclip
  class Attachment
    alias original_assign assign
    def assign(*args)
      original_assign(*args)
    rescue NotIdentifiedByImageMagickError => e
    end
  end
end

吞下这个异常似乎没问题,因为至少如果你使用 :whiny => true 就会添加验证错误。

于 2010-12-23T11:00:16.730 回答