Reputation: 1133
Here's the code I have
class FactSheet < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
And the uploader
class ImageUploader < CarrierWave::Uploader::Base
def extension_white_list
%w(jpg jpeg gif png)
end
end
This is all good, but the problem I'm facing is that in this scenario it's optional for a user to supply an image, so I don't want to be seeing the following validation error if they don't supply one
Image You are not allowed to upload "" files, allowed types: jpg, jpeg, gif, png
What's the best way to only validate/mount the uploader if the image is present?
Upvotes: 0
Views: 250
Reputation: 8710
In your FactSheet model, you could something like this:
validates :attachment, allow_blank: true, format: {
with: %r{\.(gif|jpg|png|jpeg)\Z}i,
message: 'image must be a GIF, JPEG, PNG format'
}
Upvotes: 1