Search code examples
ruby-on-railsrubycustomvalidator

Custom validations causing errors


I am new to rails and am following the Depot application in the Pragmatic Agile Web development with rails and I am having an odd problem.

In my product model I created a validator for confirming that the image asked for in the image url field actually exist as an asset. Here is my product model code.

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

Now the problem is when I run my unit test. Here is what it is:

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

But doing so triggers a bunch of errors in my code. Without the custom validtor, everything seems to work just fine. Here are the errors I am currently getting.

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

Solution

  • The problem is Rails.application.assets.find_asset(image_url), image_url is nil when you create a refresh object at test:

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

    You can find the reason of the error looking at log

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

    Here (link) the line 156 where error happens, path was nil here.

    So to solve this problem you need to add a condition at your if to check if image_url isn't nil, here is my 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