rails 3.2 skip paperclip attachment presence validation on edit action

佐手、 提交于 2019-12-12 04:46:09

问题


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:


Use on option.

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

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



来源:https://stackoverflow.com/questions/12686443/rails-3-2-skip-paperclip-attachment-presence-validation-on-edit-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!