Paperclip has pretty much become the standard when it comes to attaching files to models in Rails. It has a very easy to use API, allows the user to create her own post-processing code (such as OCR or others), and provides callbacks for before and after post-processing.This post does not cover getting started with paperclip. There are plenty of other posts that cover that. The intent is to get you up and running on using paperclip to attach non-image files.
Out of the box, the default post processor invoked upon upload is the Paperclip::Thumbnail processor. This processor creates thumbnails of an image based on the styles hash passed to the has_attached_file method. If you want to upload word documents, excel files, and other non-image data, the Thumbnail processor will fail, and the attachment will not succeed. This processor does not check if the file itself is an image, and tries to call ImageMagick's identify command on the file anyways.
Paperclip makes solving this extremely simple: prevent the post processing from happening when the file is not an image. Just like on ActiveRecord callbacks, you can return false on paperclip's before_post_process callback to avoid the processing from happening in the first place.
One possible solution is to put the following in whatever model you're using paperclip on:
before_post_process :image?
def image?
!(data_content_type =~ /^image.*/).nil?
endIn this case, we've created an image? method which returns true if the content type starts with the string image (as in 'image/png', 'image/jpeg', etc). What's neat about this is that this same method can be used in your views to either render an image_tag, or something else such as an icon or the file's name, depending on whether the file is an image.
