Ruby on rails 回形针在销毁时触发验证错误

Ruby on rails 回形针在销毁时触发验证错误,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,我的回形针型号中有以下功能: def ratiocorrect ratio = Paperclip::Geometry.from_file(image.queued_for_write[:original].path).width / Paperclip::Geometry.from_file(image.queued_for_write[:original].path).height if ratio < 1.499 or ratio > 1.501 e

我的回形针型号中有以下功能:

def ratiocorrect
    ratio = Paperclip::Geometry.from_file(image.queued_for_write[:original].path).width / Paperclip::Geometry.from_file(image.queued_for_write[:original].path).height
    if ratio < 1.499 or ratio > 1.501
      errors.add(:image,'ratio should be 4:3')
    end
  end
而且它工作得完美无瑕

但是,当我要销毁图像时,会出现以下错误:

undefined method `path' for nil:NilClass
ratio = Paperclip::Geometry.from_file(image.queued_for_write[:original].path).width / Paperclip::Geometry.from_file(image.queued_for_write[:original].path).height
它似乎再次检查了排队等待写入的
图像,因为没有用于销毁操作的图像


是否可以仅在创建或更新而不是销毁时进行验证

是的,在自定义验证中使用
:create
和/或
:update
选项

您将执行以下操作:

validate :ratiocorrect, on: :create
默认情况下,每次调用valid?时都将运行此类验证?或者保存对象。但是,也可以通过为validate方法提供一个:on选项,使用::create或:update来控制何时运行这些自定义验证

查看更多信息

validate :ratiocorrect, on: :create