Skip to content

Instantly share code, notes, and snippets.

@wangzuo
Created September 28, 2012 17:39
Show Gist options
  • Save wangzuo/3801200 to your computer and use it in GitHub Desktop.
Save wangzuo/3801200 to your computer and use it in GitHub Desktop.
carrierwave simple validation
class Subject < ActiveRecord::Base
mount_uploader :icon, IconUploader
validates_presence_of :icon
validate :image_size_validation, :image_type_validation, :if => "icon?"
def image_size_validation
errors[:icon] << "should be less than 2MB" if icon.size > 2.megabytes
end
def image_type_validation
errors[:icon] << "should be .jpg or .png file format" if (/(jpeg|jpg|png)/ =~ icon.file.content_type).nil?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment