I have a Product model which validates multiple attributes (including a Paperclip image attachment) like so:
validates_presence_of :name validates_format_of :name, :with => /^([a-zA-Z0-9\ \-]{3,128})$/i ... has_attached_file :image validates_attachment_presence :image validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png', 'image/gif']
Everything is working fine. What I want now is to make an (unobtrusive) hidden iframe in-place upload script using javascript. My problem is that I cannot just upload the image without the rest of the data, because it will fail validation (no name present) and also I cannot send the rest of the form without the image (same thing, fails validation).
So basically what I need (and don’t know how to achieve) is to conditionally apply the model validations according to what the action is currently in progress (uploading the image or editing other data).
I hope I was clear enough. Any help is appreciated. Thanks.
Railscasts have a nice video screencast about conditional validations.