I'm trying to figure out where to add errors for validations to a rails 4 app that uses geocoder.
My model looks like this:
class Tutor < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id
geocoded_by :address do |obj, results|
if geo = results.first
obj.latitude = geo.latitude
obj.longitude = geo.longitude
obj.country = geo.country
obj.city = geo.city
obj.postalcode = geo.postal_code
obj.address = geo.address
end
end
after_validation :geocode, if: :address_changed?
end
I noticed that the if geo = result.first
conditional only gets executed if the address was found successfully. I'd like to add an error message if nil is being returned. I saw that this stackoverflow thread explains that I should be using before_validation
instead of after_validation
, but I still don't understand where to add errors so that my view can get re-rendered and a valid geolocation can be input.
Any ideas where I should be putting this information? Thanks!