Make a field optional in rails

China☆狼群 提交于 2019-12-01 23:45:25

I don't know if this is gonna work but try:

validates_attachment_size :avatar, :less_than => 1.megabytes, :if => avatar_changed?
validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/png', 'image/gif'], :if => avatar_changed?

I didn't use paperclip, but in general in Rails you can add condition to decide if validations should be run.

validates_attachment_size :avatar, 
  :less_than => 1.megabytes, 
  :unless => "avatar.blank?"

You should add similar condition to all validations that affects avatar. If you want know more take a look here.

It's more like:

validates_attachment_size :avatar, :less_than => 1.megabytes, :unless => "avatar_content_type.blank?"

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