CUSTOM VALIDATION IN PAPERCLIP

In this short tutorial, I will explain you how you can add custom validation messages on the Attachment if you’re using Paperclip plugin or gem.

Way 1. This will work if you’re running your rails application on Windows Machine.
Solution: a) install mimetype-fu plugin.
b) Once installed, open Model, where paperclip settings has been defined. In my case, its User Model. Open it and add:

has_attached_file :avatar
validate :prohibited_ext
protected
def prohibited_ext
errors.add(:avatar, “Format #{File.mime_type?(avatar.path)} is invalid!”) unless File.mime_type?(avatar.path) == “application/exe”
end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s