ruby on rails - Validations with geocoder gem -


i'm trying figure out add errors validations rails 4 app uses geocoder.

my model looks this:

class tutor < activerecord::base   belongs_to :user         validates_presence_of :user_id    geocoded_by :address |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 if geo = result.first conditional gets executed if address found successfully. i'd add error message if nil being returned. saw this stackoverflow thread explains should using before_validation instead of after_validation, still don't understand add errors view can re-rendered , valid geolocation can input.

any ideas should putting information? thanks!

you can set in model example below validate address geocode called once when address changed. in geocoded_by method set explicitly write latitude , longitude when address not found columns set nil.

class company < activerecord::base    geocoded_by :address |object, results|     if results.present?      object.latitude = results.first.latitude      object.longitude = results.first.longitude     else      object.latitude = nil      object.longitude = nil     end   end    before_validation :geocode, if: :address_changed?    validates :address, presence: true   validates :found_address_presence    def found_address_presence     if latitude.blank? || longitude.blank?       errors.add(:address, "we couldn't find address")     end   end end 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -