# Form Validation Question, and a GeoKit Question

**URL:** <https://www.ruby-forum.com/t/form-validation-question-and-a-geokit-question/142409>\
**Category:** Rails\
**Created:** [July 7, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/form-validation-question-and-a-geokit-question/142409 "2008-07-07T17:51:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff.Corcoran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@Jeff.Corcoran](https://www.ruby-forum.com/u/Jeff.Corcoran)\
**Post date:** [July 7, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/form-validation-question-and-a-geokit-question/142409/1 "2008-07-07T17:51:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jeff.Corcoran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@Jeff.Corcoran](https://www.ruby-forum.com/u/Jeff.Corcoran)\
**Post date:** [July 7, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/form-validation-question-and-a-geokit-question/142409/2 "2008-07-07T18:50:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jeff.Corcoran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@Jeff.Corcoran](https://www.ruby-forum.com/u/Jeff.Corcoran)\
**Post date:** [July 8, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/form-validation-question-and-a-geokit-question/142409/3 "2008-07-08T14:46:12Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Jeff.Corcoran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@Jeff.Corcoran](https://www.ruby-forum.com/u/Jeff.Corcoran)\
**Post date:** [July 8, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/form-validation-question-and-a-geokit-question/142409/4 "2008-07-08T15:50:46Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Jeff.Corcoran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@Jeff.Corcoran](https://www.ruby-forum.com/u/Jeff.Corcoran)\
**Post date:** [March 20, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/form-validation-question-and-a-geokit-question/142409/5 "2009-03-20T16:38:12Z")

</div>

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
