Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 条件回形针存在性验证_Ruby On Rails_Validation_Activerecord_Paperclip_Paperclip Validation - Fatal编程技术网

Ruby on rails 条件回形针存在性验证

Ruby on rails 条件回形针存在性验证,ruby-on-rails,validation,activerecord,paperclip,paperclip-validation,Ruby On Rails,Validation,Activerecord,Paperclip,Paperclip Validation,如果存在其他字段,如何验证回形针附件不存在?我试过: validates_attachment :img, presence: false, if: :some_other_field? def some_other_field? some_other_field end 试试这个:- validates_attachment :img, presence: true, if: :some_other_field? def some_other_field? some_other_fi

如果存在其他字段,如何验证回形针附件不存在?我试过:

validates_attachment :img, presence: false, if: :some_other_field?
def some_other_field?
  some_other_field
end
试试这个:-

validates_attachment :img, presence: true, if: :some_other_field?
def some_other_field?
  some_other_field.present?
end

您是否尝试过使用
存在?
而不是
存在?
,可能有效

validates_attachment :img, presence: false, if: :some_other_field?
def some_other_field?
  some_other_field.exists?
end

类似的问题,我的解决方案是在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

就像TechVinet的方法和Aman的方法一样,我试过了。没有骰子。这是我决定的,但我仍然在寻找一种更优雅的方式:
验证:img\u文件名,缺勤:true,if::some\u other\u字段?