You’re most likely going to deal with file uploads within your Rails application. If you haven’t yet, it’s coming and hopefully this will save you some time. I spent about an hour today racking my brain as to why I couldn’t validate different image types: image/jpeg, image/gif, and image/png. I was doing a simple string check that looked like this:
def check_format
content_type = params[:photo].content_type
if content_type != “image/pjpeg” && content_type != “image/jpeg” && content_type != “image/gif” && content_type != “image/png”
“Error: You’ve specified the wrong file type”
end
end
This was failing repeatedly and even printing the content_type variable would yield image/jpeg. It seemed that everything should work perfectly. The only thing I could fathom is that there may be some extraneous garbage at the end of the string. After doing a nice ruby chomp this solved the problem:
content_type = params[:photo].content_type.chomp
I hope this saves you some frustration in the future.
damn… I wasted 2 hours no this. Thanks!
I like to do it like this, using some built in feaurtes of ruby seems so much easier for me
acceptable_types = ['image/jpeg', 'image/gif', 'image/png', ...etc..]
“Err” unless acceptable_types.include?params[:photo].content_type.chomp
I wasted hours with this problem, then I googled “rails file content_type” and this came up. This needs to be filed as a bug.