rails 3.2 在编辑操作时跳过回形针附件存在验证 [英] rails 3.2 skip paperclip attachment presence validation on edit action

查看:48
本文介绍了rails 3.2 在编辑操作时跳过回形针附件存在验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 的新手,我正在开发我的第一个使用回形针作为附件的 Rails 应用程序,我正在使用 validates_attachment :avatar,presence: true 验证附件的存在,该应用程序正在运行创建操作非常好,但是当用户正在编辑他的帐户设置并且没有上传新头像时,没有上传头像会出现错误,但我想跳过编辑时的验证,如果用户上传,则仅在编辑时验证一个新的来取代现有的.我也有

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,:除非=>Proc.new {|m|m[:avatar].nil?}, :message =>"请只上传以下扩展名的文件

仅在存在时检查格式但似乎也不起作用非常感谢您的帮助.谢谢

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

推荐答案

使用选项.

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

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

这篇关于rails 3.2 在编辑操作时跳过回形针附件存在验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆