0

我想运行这些命令:

require 'fileutils'
FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/tmp/*")

如果通过回调在我的模型中验证失败:

我见过回调after_validation

如何通过after_validation回调检查验证是否正常或验证不正确(KO)?

4

3 回答 3

2

你可以检查

if your_model.errors.empty?
   # run commands
end
于 2012-06-09T11:35:58.040 回答
2
class User < ActiveRecord::Base

  after_validation :do_something_if_validation_fails, :unless => lambda { errors.empty? }

  protected
  def do_something_if_validation_fails
    logger.info "Validation failed lets do something"
  end
end
于 2012-06-09T11:46:04.340 回答
0

我的解决方案是:

after_validation :empty_tmp

  protected
  def empty_tmp
   if self.errors.any?
    require 'fileutils'
    FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/tmp/*")
   end
  end
于 2012-06-09T12:09:40.393 回答