0
votes

I am new to Rails and i'm working on my first rails application that uses paperclip for attachments and i'm validating the presence of attachment using validates_attachment :avatar, presence: true which is working perfectly well on create action, but when a user is editing his account settings and doesn't upload a new avatar, there is an error for not having avatar uploaded but i want to skip that validation on edit and only validate on edit if user uploads a new one to replace existing one. i also have

validates_format_of :avatar, :with => %r{\.(jpg|jpeg|gif|png)$}i,:unless => Proc.new {|m| m[:avatar].nil?}, :message => "Please upload files with the following extensions only

to check format only if present but doesn't seem to be working either your help would be very much appreciated. thanks

1

1 Answers

2
votes

Use on option.

validates_format_of :avatar ... , :on => :create

http://guides.rubyonrails.org/v2.3.11/activerecord_validations_callbacks.html#on