bryce benton
2005-Jul-20 12:54 UTC
observe_field: how to update a text field? how to respond only after 5 characters in observed field?
How do I pass the value returned from observe_field into the "value"
part of the updated text field?
Also, how would I limit the observation so that it only responds after
the observed field has 5 characters?
''
Basically, I want to update the "city" field after 5 digits are given
in the "zipcode" field. In some rare cases, the actual city/zipcode
pair may not match the pair in my database, so I want to give users
the option of editing the city field if necessary.
Thanks,
Bryce
p.s. Hey lazyweb... wouldn''t this make a cool ruby gem? For anyone
looking to find a free database of United States zipcodes with cities,
states, lattitude, and longitude, you''ll find it here:
http://civicspacelabs.org/home/developers/download
----
Here''s what I came up with so far:
### view:
<form>
zip:<br/>
<%= text_field(''address'',''zip'') %>
<br/>
<br/>
city:<br/>
<%= text_field(''address'',''city'') %>
<p id="test">...</p>
<%= observe_field(''address_zip'', :frequency=>1,
:update=>''address_city'',
:url=>{:controller=>''store'',
:action=>''get_city_from_zip''},
:with=>"''zip=''+value"
) %>
</form>
### get_city_from_zip.rhtml
<%= @city.city %>
### controller
def get_city_from_zip
@city = Zipcode.find(:first, :conditions=>[''zipcode =
?'', params[:zip]])
render(:layout => false)
@city.city
end