Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialIvan Bechev
851 PointsError when trying to upload image file with Paperclip.
Hi everyone. I;m stuck with that error, it doesn't allow me to upload image file to the server. The message I'm getting is: "1 error prohibited this picture from being saved: Asset has an extension that does not match its contents"
Here is my model:
class Picture < ActiveRecord::Base
belongs_to :album
has_attached_file :asset, styles: {
large: "800x800>", medium: "300x200>", small: "260x180>", thumb: "80x80#"
}, :default_url => "/images/:style/missing.png"
validates_attachment :asset, :content_type => { :content_type => ["image/jpeg", "image/gif", "image/png"] }
end
I was trying to find solution, but so far have to success. I;ll very much appreciate if anyone can show me the solution, Thank you in advance.
Regards Ivan
6 Answers
Tim Knight
28,888 PointsIt seems to me you do have a bug on your validates_attachment call you're including an array called content_type into a hash called content_type... you can adjust that to something like this
validates_attachment_content_type :asset, :content_type => ["image/jpeg", "image/gif", "image/png"]
Something I'll often do would be to set the content_type with a regular expression for all image mimetypes
validates_attachment_content_type :asset, :content_type => /\Aimage\/.*\Z/
Tim Knight
28,888 PointsIvan, try uploading your error to something like https://imgur.com/ to share it on the forum.
Ivan Bechev
851 PointsHere is a link to a folder in my Dropbox. I've uploaded snapshots from my application there.
https://www.dropbox.com/sh/qckpj94qxezeul3/AAAykabwyR1KCzdx10sJ49NAa
Thanks
Tim Knight
28,888 PointsSeems there's an issue with some images when it comes to content type validation. There's a workaround on https://github.com/thoughtbot/paperclip/issues/1429 that involves creating an initializer.
Michael Rossiter
2,715 PointsI'm having the exact same issue - going to look into the initializer. Can see my problem here: http://pure-sierra-6675.herokuapp.com/paperclip_images
Michael Rossiter
2,715 PointsA couple things are going on:
- It works on Heroku for some images, but not all. For example, my downloaded LinkedIn image does not work. Most other images seem to.
- It does not work on localhost:3000. Not sure why. I just installed imagemagick, which I was hoping work. It did not. Spoofing does seem to be the issue.
It would be helpful to have a more beginner-friendly explanation of the github discussion.
Ivan Bechev
851 PointsIvan Bechev
851 PointsI've changed my code , but it still gives me the same error when I try to upload image.
By the way I'm using Windows 7 , if that matters,