Rails validation issue with before_validation

Posted by Chance on Stack Overflow See other posts from Stack Overflow or by Chance
Published on 2010-06-05T16:13:31Z Indexed on 2010/06/05 16:22 UTC
Read the original article Hit count: 162

Filed under:
|
|

I'm still fairly new to rails so I'm not sure what I'm missing here.

I'm using GeoKit to geocode an address upon saving. I have a method that geocodes an address and if it fails to find it, it adds an error to the errors list. I've tested it in the console and it is failing on the geocode (presumably adding the error) but still saving successfully.

acts_as_mappable
before_validation_on_create :geocode_address
before_validation_on_update :geocode_address
validates_presence_of :street
validates_presence_of :city
validates_presence_of :state
validates_presence_of :zip
validates_presence_of :name
validates_uniqueness_of :name

 def geocode_address
    geo=Geokit::Geocoders::MultiGeocoder.geocode ("#{street}, #{city}, #{state}, #{zip}")
    puts "geocoded: #{street}, #{city}, #{state}, #{zip}"
    if geo.success
      self.lat, self.lng = geo.lat,geo.lng
    else
      errors.add(:street, "Could not Geocode address")
    end 
   puts "geo status: #{geo.success}"
 end

Any help would be greatly appreciated, thanks :)

© Stack Overflow or respective owner

Related posts about ruby

Related posts about validation