views:

114

answers:

1

I'm using paperclip to upload all sorts of files (text documents, binaries, images).

I'd like to put this in my model:

has_attached_file :attachment, :styles => { :medium => "300x300>", :thumb => "100x100>" }

but it has to perform the styles only if it's an image. I tried adding

if :attachment_content_type =~ /^image/

but it didn't work.

+1  A: 

You can use before_<attachment>_post_process callback to halt thumbnail generation for non-images. If you return false in callback, there will be no attempts to use styles.

See "Events" section in docs

  before_attachment_post_process :allow_only_images

  def allow_only_images
    if !(attachment.content_type =~ %r{^(image|(x-)?application)/(x-png|pjpeg|jpeg|jpg|png|gif)$})
      return false 
    end
  end 
Voyta