<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rise of the PHX &#187; Adventures on the Rails</title>
	<atom:link href="http://riseofthephx.com/category/adventures-on-the-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://riseofthephx.com</link>
	<description>Web musings in the Valley of the Sun.</description>
	<lastBuildDate>Mon, 19 Jul 2010 18:29:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting observe_field to update a text_field in Rails</title>
		<link>http://riseofthephx.com/2009/11/13/getting-observe_field-to-update-a-text_field-in-rails/</link>
		<comments>http://riseofthephx.com/2009/11/13/getting-observe_field-to-update-a-text_field-in-rails/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 23:24:44 +0000</pubDate>
		<dc:creator>Gregg</dc:creator>
				<category><![CDATA[Adventures on the Rails]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[observe_field]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[text_field]]></category>

		<guid isPermaLink="false">http://riseofthephx.com/?p=118</guid>
		<description><![CDATA[
I am writing this post as I scoured page after page with out luck and came up with this solution.  Others may already be all over this method or have more eloquent solutions but there people didn&#8217;t have their content search engine optimized.  So for those in need here ya go.
I have used observe_field on [...]]]></description>
			<content:encoded><![CDATA[<p><img style="float:left;padding:11px;" title="Square Peg in a Round Hole_0565" src="http://riseofthephx.com/wp-content/uploads/2009/11/square-peg-round-hole.jpg" alt="Square Peg in a Round Hole_0565" width="290" height="414" /></p>
<p>I am writing this post as I scoured page after page with out luck and came up with this solution.  Others may already be all over this method or have more eloquent solutions but there people didn&#8217;t have their content search engine optimized.  So for those in need here ya go.</p>
<p>I have used observe_field on occasion in great success to update select form elements in form_for and form_tags but ran into trouble an when trying to have one text_fields value influence the value of another text_fields.</p>
<h3>The Goal</h3>
<p>I wanted to auto populate the city in a text_field  based on the zip code information provided  in the text_field above.  With the city and zip code I am planning  to build a layered Google maps component ( but that is for a later post).  For now if the city could pre-populate based on zipcode provided, and allow for corrections if the user wanted to put in a different city, then I win&#8230;.  streamers and confetti fall from the ceiling.</p>
<h3>What Didn&#8217;t Work</h3>
<blockquote><p>&lt;p&gt;<br />
&lt;%= f.label :zipcode, &#8220;Zip code&#8221; %&gt;: &lt;%= f.text_field :zipcode, :size=&gt;&#8217;8&#8242; %&gt;<br />
&lt;/p&gt;</p>
<p id="city_lookup">&lt;p&gt;<br />
&lt;%= f.label :city, &#8220;City (change if zip code lookup is incorrect): &#8221; -%&gt;&lt;%= f.text_field :city -%&gt;<br />
&lt;/p&gt;</p>
<p>&lt;%= observe_field &#8216;community_zipcode&#8217;,:method =&gt; :get,:frequency =&gt; 0.25,:update =&gt; &#8216;community_city&#8217;,:url =&gt; {:controller=&gt;&#8217;communities&#8217;,:action =&gt; &#8216;getcity&#8217;},:with =&gt; &#8220;zipcode&#8221; -%&gt;</p></blockquote>
<p>Notice above that I tried to reference the DOM id of the text_field I was interested in updating directly.  It did not work as I wished so I moved on to plan B.</p>
<h3>Plan B</h3>
<p>Plan B involved creating a partial &#8216;_city_lookup.html.erb&#8217; with contents:</p>
<blockquote><p>&lt;label for=&#8221;community_city&#8221;&gt;City (change if zip code lookup is incorrect):&lt;/label&gt;&lt;input id=&#8221;community_city&#8221; name=&#8221;community[city]&#8221; size=&#8221;30&#8243; type=&#8221;text&#8221; value=&#8221;&lt;%=@city%&gt;&#8221; /&gt;</p></blockquote>
<p>The method or action, in communities_controller.rb is  now set as follows:</p>
<blockquote><p>def getcity<br />
unless request.xhr?<br />
flash[:error] = &#8216;Invalid page&#8217;<br />
redirect_to &#8220;/&#8221;<br />
else<br />
location = ZipCode.find(:first,:conditions=&gt;['zip=?',params[:zipcode].to_i]) rescue nil<br />
@city= location.blank? ? &#8216;no zipcode match&#8217; : location.city<br />
render :partial=&gt;&#8217;city_lookup&#8217;, :layout =&gt; false, :locals =&gt; {:city =&gt; @city}<br />
end<br />
end</p></blockquote>
<p>The new view code, notice the &#8216;id&#8217; of &#8216;city_lookup&#8217; now on the paragraph holding the text_field we are interested in updating.  Our partial now will replace the contents of this paragraph when a change is made in the zip code field.</p>
<blockquote><p>&lt;p&gt;<br />
&lt;%= f.label :zipcode, &#8220;Zip code&#8221; %&gt;: &lt;%= f.text_field :zipcode, :size=&gt;&#8217;8&#8242; %&gt;<br />
&lt;/p&gt;<br />
&lt;p id=&#8221;city_lookup&#8221;&gt;<br />
&lt;%= f.label :city, &#8220;City (change if zip code lookup is incorrect): &#8221; -%&gt;&lt;%= f.text_field :city -%&gt;<br />
&lt;/p&gt;<br />
&lt;%= observe_field &#8216;community_zipcode&#8217;,:method =&gt; :get,:frequency =&gt; 0.25,:update =&gt; &#8216;city_lookup&#8217;,:url =&gt; {:controller=&gt;&#8217;communities&#8217;,:action =&gt; &#8216;getcity&#8217;},:with =&gt; &#8220;zipcode&#8221; -%&gt;</p></blockquote>
<p>That&#8217;s all folks.  I will update once site is online for working examples.</p>
]]></content:encoded>
			<wfw:commentRss>http://riseofthephx.com/2009/11/13/getting-observe_field-to-update-a-text_field-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
