[Paperclip]Attach non-image files in Rails3

ref. Attach Non Image Files in Rails With Paperclip:
http://practiceovertheory.com/blog/2009/05/19/attach-non-image-files-in-rails-with-paperclip/

이미지가 아닌 문서 파일을 업로드할 경우 Paperclip gem 패키지에서 에러가 발생함.
before_post_process 메소드를 이용해 처리한다.

class Attachment < ActiveRecord::Base
  attr_accessible :asset
  #paperclip: file attachments
  has_attached_file :asset, :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
                     :default_url => "/images/:style/missing.png",
                     #:url => "/system/:class/:attachment/:id_partition/:style/:filename",
                     #:url => "/system/:class/:attachment/:id/:style_:hash.:extension",
                     #:hash_secret => "rmdwjddmlgla"
                     :url => "/system/:class/:attachment/:id/:style/:updated_at.:extension"

  validates :asset, :attachment_presence => true
  before_post_process :image?
  
  def image?
    !(asset_content_type =~ /^image.*/).nil?
  end
  
end

Leave a Reply