Paperclip 上傳驗證

因為一些原因,需要驗證使用者是否有上傳照片,若有上傳照片才需要驗證檔案大小、檔案格式

# model.rb

validates_attachment_size :avatar, {
  less_than: 300.kilobytes, if: Proc.new{|imports| imports.avatar.uploaded_file.present?}
}

validates_attachment_content_type :avatar, {
  content_type: ["image/png", "image/jpeg", "image/jpg"],
  if: Proc.new{|imports| imports.avatar.uploaded_file.present?}
}

Comments

comments powered by Disqus