Need help debugging syntax error from Agile Web Development, 3RD Edition

Hi--

I'm having trouble with a syntax error, which I'm working with on page
89 of Agile Web Development. I'm getting the following syntax error:

/Users/pdenlinger/Sites/depot/app/models/product.rb:4: syntax error,
unexpected tSYMBEG, expecting kDO or '{' or '('
  validates_numericality_of :price
                             ^
/Users/pdenlinger/Sites/depot/app/models/product.rb:9: syntax error,
unexpected tASSOC, expecting kEND
... :message => 'must be a URL for GIF, JPG' ...
                              ^

The original code I have on the model (product.rb) is:

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'

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

Thanks for your help.

Paul

That’s an easy one, really. You got a typo.

As far as I can see there is an extra ‘,’ (comma) at the end of the first statement, the statement just before your validates_numericality_of. I have indicated that below within []. Removing that should solve your issue.

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’

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

Thanks & Regards,
Dhruva Sagar.

Thank you.

I'm still getting an error for the second statement:

/Users/pdenlinger/Sites/depot/app/models/product.rb:9: syntax error,
unexpected tASSOC, expecting kEND
... :message => 'must be a URL for GIF, JPG' ...
                              ^

The code is as follows:

validates_format_of :image_url,
                      :with => %r{\.(gif|jpg|png$)}i
                      :message => 'must be a URL for GIF, JPG' + 'or
PNG image'

Thanks,
Paul

I know it is difficult when just starting, but I would have thought
that following the previous problem with a comma that you could have
worked out this one yourself. When you see a syntax error the problem
saying something unexpected found that means that there is likely an
error a bit before the point noted. The basic syntax of
validates_format_of (or any function for that matter) is
validates_format_of <parameter>, <parameter>, ... :image_url is the
first parameter, :with => ... is the second and :message => .. is the
third. I am sure you can see the error, or at least the one
generating the message.

Colin

Hi Paul,

This is the same error as the previous one, but in reverse. :wink:

Cheers,

Tom

Hi Colin--

Thanks. Got the errors.

Two missing commas. Will try to avoid repeating that mistake in the
future.

Paul