自定义验证导致错误

时间:2013-01-09 23:55:26

标签: ruby-on-rails ruby customvalidator

我是rails的新手,我正在使用rails跟踪Pragmatic Agile Web开发中的Depot应用程序,我遇到了一个奇怪的问题。

在我的产品模型中,我创建了一个验证器,用于确认图像url字段中要求的图像实际上是作为资产存在的。这是我的产品型号代码。

class Product < ActiveRecord::Base
  attr_accessible :description, :image_url, :price, :title
  validates :description, :price, :title, :presence => true
 validate :image_url_exists, on: :create
  def image_url_exists
    if Rails.application.assets.find_asset(image_url) == nil
      errors.add(:image_url, 'is not valid. The image does not exist.')
    end
  end
end

现在问题出在我运行单元测试的时候。这就是它:

require 'test_helper'
    class ProductTest < ActiveSupport::TestCase
       test "the products attributes should not be empty" do
         p = Product.new
         assert p.invalid?
       end
    end

但这样做会在我的代码中触发一堆错误。没有自定义验证器,一切似乎都运行得很好。以下是我目前遇到的错误。

test_the_products_attributes_should_not_be_empty(ProductTest):
TypeError: can't convert nil into String
    /var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `initialize'
    /var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `new'
    /var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `find_asset'
    /var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/index.rb:60:in `find_asset'
    /var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/environment.rb:78:in `find_asset'
    /home/saurabh/Desktop/SCRIPTS/Rails/depot/app/models/product.rb:6:in `image_url_exists'
    /var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:418:in `_run__4343689776242734370__validate__107120755283260520__callbacks'
    /var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:405:in `__run_callback'
    /var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
    /var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations.rb:228:in `run_validations!'
    /var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
    /var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:403:in `_run__4343689776242734370__validation__107120755283260520__callbacks'
    /var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:405:in `__run_callback'
    /var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
    /var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
    /var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations.rb:195:in `valid?'
    /var/lib/gems/1.9.1/gems/activerecord-3.2.9/lib/active_record/validations.rb:69:in `valid?'
    /var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations.rb:203:in `invalid?'

1 个答案:

答案 0 :(得分:5)

问题是Rails.application.assets.find_asset(image_url)image_url在测试时创建刷新对象时为nil

p = Product.new # Here p.image_url => nil

您可以找到查看日志的错误原因

/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `initialize'

此处(link)发生错误的第156行,path此处为nil

因此,要解决此问题,您需要在if添加条件,以检查image_url是否为零,这是我的消化

if self.image_url.nil? || Rails.application.assets.find_asset(self.image_url).nil?
  errors.add(:image_url, 'is not valid. The image does not exist.')
end