small medium large xlarge

31 Jul 2008, 20:39
Bruce C. Martin III (5 posts)

I have just started working with AWDR and have the beta of the third edition. I have run into a problem when trying to validate the image url. Here is what I have in the product.rb file, excluding the begining and ending quotes.

` ~~~ class Product < ActiveRecord::Base validates_presence_of :title, :description, :image_url validates_numericality_of :price validate :price_must_be_at_least_a_cent validates_uniqueness_of :title validates_format_of :image_url :with => %r{.(gif|jpg|png)$}i, :message => “must be a URL for GIF, JPG or PNG image.(gif/jpg/png)”

protected def price_must_be_at_least_a_cent errors.add(:price, ‘should be at least 0.01’)if price.nil? || price < 0.01
end end ~~~ `

but I end up with the follow error when I refresh the page.

C:/Ruby_Test/depot/app/models/product.rb:7: syntax error, unexpected tASSOC, expecting kEND :with => %r{.(gif|jpg|png)$}i, ^ C:/Ruby_Test/depot/app/models/product.rb:7: syntax error, unexpected ‘,’, expecting kEND

did I miss something in the book?


29 Apr 2008, 20:17
Sam Ruby (633 posts)

There needs to be a comma (“,”) at the end of the line that reads “validates_format_of :image_url,”. Without that comma, Ruby thinks that the statement is complete, and gets confused when it attempts to process the next line.

30 Apr 2008, 17:49
Bruce C. Martin III (5 posts)

That’s it for some reason my mind looked right over the comma. It’s in the text plain as day. Thanks.


31 Jul 2008, 04:19
Chris Bloom (4 posts)

Huh, I was getting an error about an kINTEGER instead of a kEND until I came here and found out that it is supposed to be an “i” at the end of the regexp. I swear it looks like a “1” when viewing the PDF at 67%

You must be logged in to comment