Paperclip::NotIdentifiedByImageMagickError when file is not a valid attachment content type
I have systematically an error when I'm trying to upload a file that is not in ["image/jpg", "image/jpeg", "image/gif", "image/png", "image/pjpeg", "image/x-png"]
When I try to upload a file like a 'wav' I have this message
* 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
So it detects that the file is not an image and display my message "Accepted files include: jpg, gif, png"
but I have this extra message included before mine Photo is not recognized by the 'identify' command...
Upload works fine for pictures
My code is:开发者_开发技巧
Controller:
def upload
@picture= Picture.new(params[:picture])
if !@picture.valid?
render :form
end
end
View form:
<%= 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 %>
Picture model:
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
solved it with :whiny => false
has_attached_file :photo, :whiny => false, :styles => { :medium => "300x300>", :thumb => "100x100>" }
:whiny => false was not enough to solve the problem with the latest version of paperclip (2.3.6). I ended up doing this in a rails initializer:
module Paperclip
class Attachment
alias original_assign assign
def assign(*args)
original_assign(*args)
rescue NotIdentifiedByImageMagickError => e
end
end
end
It seems okay to swallow that exception because validation errors get added anyway at least if you use :whiny => true.
精彩评论