Form Validation Question, and a GeoKit Question

Yes - this is a re-post, and my last post to this question - but I was
just taking one last attempt at getting help.

Any answers on how to do either would be greatly appreciated.

I am new to rails and have been scouring the web for the past two days
trying to figure out 2 things.

  1. I am currently using a form with 2 models. The first is person, the
    second is address. I attempted to duplicate the two models on one form
    as seen in Advanced Rails Recipes and Railscasts, and they work fine -
    except when there are validation issues on the address piece it just
    returns saying “Addresses is invalid”. I would much rather it say
    “Street Address is blank” or “City cannot be blank” any ideas on how
    to do this?

  2. In using the GeoKit plugin I am attempting to use the
    acts_as_mappable piece so when I create an address it adds the lat and
    lng to the DB. If I choose just 1 field and specify it this works fine
    and the table populates, but I want to concat all of the form values
    (Street_Address_1, City, State, Postal_Code, Country) and pass these
    in as the location. It appears GeoKit expects address to be a single
    field - which will not work for my application. I am not opposed to
    using another plugin, it just seems GeoKit is overall the easiest.

Any help would be greatly apperciated, and please go easy on me, I am
attempting to learn RoR on and old AWDWR book, and Advanced Rails
Recipies.

to your first question:

you can either define a message for the validate_* functions, so for
example:

validate_presence_of :city
validate_presence_of :street, {:message => “your specific error
message”}

OR

if you wanna do your own validation:

def validate
errors.add(:my_variable, “your specific error message”) if
validate_my_variable
errors.add(:my__other_variable, “your specific error message”) if
validate_my_other_variable
end

i dont have any experience with geokit, so no help from me
there…sorry

hope this helps! smn

  1. I am currently using a form with 2 models. The first is person, the
    second is address. I attempted to duplicate the two models on one form
    as seen in Advanced Rails Recipes and Railscasts, and they work fine -
    except when there are validation issues on the address piece it just
    returns saying “Addresses is invalid”. I would much rather it say
    “Street Address is blank” or “City cannot be blank” any ideas on how
    to do this?

It sounds like you have
validates_associated :address

On your Person class. That validation works by calling valid? on the
associated object and it’s default message is “is invalid”. It just
doesn’t know any better.

I’d also guess that you have
<%= error_messages_for :person %>

in your view. The result is that you render the errors for the
person… and it only knows that address is invalid. If you want more
you’ll have to give Rails permission to show more. The simplest way
would be to add address to the list of objects for which you render
errors:

<%= error_messages_for :person, :address %>

  1. In using the GeoKit plugin I am attempting to use the
    acts_as_mappable piece so when I create an address it adds the lat and
    lng to the DB. If I choose just 1 field and specify it this works fine
    and the table populates, but I want to concat all of the form values
    (Street_Address_1, City, State, Postal_Code, Country) and pass these
    in as the location. It appears GeoKit expects address to be a single
    field - which will not work for my application. I am not opposed to
    using another plugin, it just seems GeoKit is overall the easiest.

You don’t need to have one big address field, only provide something
like it for geocoding. I have similar needs in one app and solved it
like this:

class Address < ARec::Base
validates_presence_of :street, :city, :state, :zip_code
validates_numericality_of :lat, :lng

acts_as_mappable
before_validation_on_create :generate_geocode

private
def generate_geocode
address = [self.street, self.city, self.state,
self.zip_code].join(’, ')
geoloc = GeoKit::Geocoders::MultiGeocoders.geocode(address)
errors.add_to_base(‘Could not geocode address’) unless geo.success
self.lat, left.lng = geoloc.lat, geoloc.lng
end
end

HTH,
AndyV

You are amazing! Thanks!

I was trying to find a way of joining all of that information together
for the geokit, trying to use a concat function and it didn’t work out
to well.

I will have to try the validation piece later on I hope that works!

Thanks for that, nice little method, this is what I put in:

acts_as_mappable

validates_numericality_of :lat, :lng

before_validation_on_create :generate_geocode

private
def generate_geocode
address = [self.street, self.city, self.location_state,
self.postcode, self.country].join(’, ')
geoloc = GeoKit::Geocoders::MultiGeocoder.geocode(address)
errors.add_to_base(‘Could not geocode address’) unless
geoloc.success
self.lat, self.lng = geoloc.lat, geoloc.lng
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs