2013-01-09 208 views
1

我是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 

但這樣做會觸發我的代碼中的一堆錯誤。沒有自定義validator,一切似乎都很好。這是我目前得到的錯誤。

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?' 
+3

嗯,我不這麼認爲。我認爲'::create'是有效的ruby語法。 – user1000232

+0

'如果Rails.application.assets.find_asset(image_url).nil?' –

回答

5

問題是Rails.application.assets.find_asset(image_url)image_urlnil當你在測試創建刷新對象:

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不爲零,這是我sugestion

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