small medium large xlarge

25 Jan 2012, 21:16
Megan Nissen (1 post)

Apologies if this has already been posted (I didn’t seem to find it when searching the forums).

On page 80 the Product class validations read:

class Product < ActiveRecord::Base validates :title, :description, presence: true validates :price, numericality: {greater_than_or_equal_to: 0.01} validates :title, uniqueness: true validates :image_url, allow_blank: true, format: { with: %r(.(gif|jpg|png)$)i, message: ‘must be a URL for GIF, JPG, or PNG image.’ } end

In particular, image_url is allowed to be blank. However, the test on page 83 seems to want the model to throw an error if the there is no image_url:

test “product attributes must not be empty” do product = assert product.invalid? assert product.errors[:title].any? assert product.errors[:description].any? assert product.errors[:price].any? # assert product.errors[:image_url].any? end

This test fails because the image_url is, in fact, allowed to be blank. Is this an error or am I doing something wrong?

Thanks, Megan Techcycled

25 Jan 2012, 22:57
Sam Ruby (634 posts)

The code on page 80 reads (note the presence of :image_url):

validates :title, :description, :image_url, :presence => true
You must be logged in to comment