4

如果另一个字段确实存在,我如何验证回形针附件不存在?我试过:

validates_attachment :img, presence: false, if: :some_other_field?
def some_other_field?
  some_other_field
end
4

3 回答 3

2

这里有类似的问题,我的解决方案是在def中进行比较

validate :check_image_with_title 

def check_image_with_title
   if !ctitle.blank? and cimage.blank?
      #If ctitle IS NOT empty and cimage IS empty, add a custom error message
      errors.add :key, "You need an image to go with your title"
      return false
    else
      return true
   end 
end
于 2013-10-02T09:33:14.463 回答
1

试试这个:-

validates_attachment :img, presence: true, if: :some_other_field?
def some_other_field?
  some_other_field.present?
end
于 2013-08-26T13:02:58.733 回答
0

您是否尝试过使用exists?present?可能有效

validates_attachment :img, presence: false, if: :some_other_field?
def some_other_field?
  some_other_field.exists?
end
于 2013-08-26T13:21:09.730 回答