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 trial

Ruby

Error 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
Tim Knight
28,888 Points

It 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/

I'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,

Tim Knight
Tim Knight
28,888 Points

Ivan, try uploading your error to something like https://imgur.com/ to share it on the forum.

Here 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
Tim Knight
28,888 Points

Seems 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
Michael Rossiter
2,715 Points

I'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
Michael Rossiter
2,715 Points

A couple things are going on:

  1. It works on Heroku for some images, but not all. For example, my downloaded LinkedIn image does not work. Most other images seem to.
  2. 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.