I'm new to Paperclip and it all seemed to work pretty fast. I'm trying to get the user to only upload PNG's or JPG's though, and although I'm uploading a JPG and my content_type validates for JPG it says it's invalid nonetheless.
I've tried removing the PNG content_type, to no avail.
I've tried using has_attached_file
aswell, but it seems to ignore the :content_type and stuff. Because if I upload a JPG with :content_type => "image/png"
only; It doesn
t give an error.
Any suggestions?
validates_attachment :avatar, :styles => {
:medium => "300x300",
:thumb => "100x100"
}, :content_type => {
:content_type => "image/jpg",
:content_type => "image/png"
},
:size => { :in => 0..1.megabytes }
Oh, and while I'm at it; I want to get my thumb and medium to the fixed width. So no scaling like 100x80 but just 100x100 either way. How can I do that?
It seems you're trying to use some Paperclip validation
, which you could look at here: Paperclip - Validate File Type but not Presence
According to that answer, you could use this:
validates_attachment_content_type :sound, :content_type => ['audio/mp3', 'application/x-mp3'], :if => :sound_attached?
An alternative is to use lambda
to check to see whether you're dealing with a specific content type. Here's an example from one of our live apps:
has_attached_file :attachment,
styles: lambda { |a| a.instance.is_image? ? {:small => "x200>", :medium => "x300>", :large => "x400>"} : {:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10}, :medium => { :geometry => "300x300#", :format => 'jpg', :time => 10}}}
def is_image?
attachment.instance.attachment_content_type =~ %r(image)
end
Lambda is only really a way to gauge whether the content-type
is what you need (I.E you only allow JPG images). In order to validate the presence of an image (rather than a video), you'd need to validates_attachment_content_type
I ran into the same issue tonight, and it turned out I needed image/jpeg and image/jpg
validates_attachment :avatar, :styles => {
:medium => "300x300",
:thumb => "100x100"
}, :content_type => {
:content_type => "image/jpg",
:content_type => "image/jpeg",
:content_type => "image/png"
},
:size => { :in => 0..1.megabytes }
It worked when just trying /^image/ but I like being specific.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With