<?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>Sean&#039;s Blog &#187; states</title>
	<atom:link href="http://seanbehan.com/tag/states/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Wed, 18 Jan 2012 21:44:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Onchange Event Fired from Select Field in Rails Form</title>
		<link>http://seanbehan.com/javascript/onchange-event-fired-from-select-field-in-rails-form/</link>
		<comments>http://seanbehan.com/javascript/onchange-event-fired-from-select-field-in-rails-form/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 15:45:21 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[carmen]]></category>
		<category><![CDATA[countries]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[onchange]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[states]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=840</guid>
		<description><![CDATA[In the view there is a regular Rails form and a javascript function that will be triggered when the country select field is changed. The javascript function will make an ajax request to the country_select url with the country code passed as the id variable, e.g., /country_code/us for the United States. I&#8217;m also using the [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fjavascript%2Fonchange-event-fired-from-select-field-in-rails-form%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Onchange Event Fired from Select Field in Rails Form" data-url="http://seanbehan.com/javascript/onchange-event-fired-from-select-field-in-rails-form/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>In the view there is a regular Rails form and a javascript function that will be triggered when the country select field is changed. The javascript function will make an ajax request to the country_select url with the country code passed as the id variable, e.g., /country_code/us for the United States. I&#8217;m also using the Carmen plugin for this example which will provide a list of countries and their respective states/provinces. Not all countries are full supported. More information on Carmen can be found at <a href="http://autonomousmachine.com/2009/4/1/carmen-a-rails-plugin-for-geographic-names-and-abbreviations">http://autonomousmachine.com/2009/4/1/carmen-a-rails-plugin-for-geographic-names-and-abbreviations</a> and <a href="http://github.com/jim/carmen">http://github.com/jim/carmen</a></p>
<pre class="wp-code-highlight prettyprint">&amp;lt;%form_for(@model) do |f| %&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&amp;gt;
  function change_state_select(state_code)
 {
    new Ajax.Request('/country_select/'+state_code,
    {
      method: 'get',
      onSuccess: function(transport) {
        $('state_select').replace(transport.responseText);
      }
    });
  }
 &amp;lt;/script&amp;gt;
 &amp;lt;%= f.select :country,
   Carmen::COUNTRIES,
   {},
   { :onchange =&amp;gt; &quot;change_state_select(this.options[this.selectedIndex].value);&quot; }
%&amp;gt;
&amp;lt;div id='state_select'&amp;gt;&amp;lt;/div&amp;gt;</pre>
<p>***Note the : onchange should really be one word but an emoticon shows up otherwise :onchange :(***<br />
Since not all countries are supported I need to execute some conditional logic in the action country_select. If the country is supported I&#8217;ll return a snippet of html containing a select field that my form will use. If the country is not supported I&#8217;ll return a text field so that the user can write in their state/province.</p>
<pre class="wp-code-highlight prettyprint">class CountrySelectController &amp;lt; ApplicationController
  def country_selecet
      begin
         @states = Carmen::states(params[:id])
      rescue
         @states = nil
      end
      render :partial =&amp;gt; &quot;country_select/states&quot;
  end
end</pre>
<p>In the final partial that is rendered there is either a select field or a text field</p>
<pre class="wp-code-highlight prettyprint">&amp;lt;div id=&quot;state_select&quot;&amp;gt;
  &amp;lt;% if @states.nil? %&amp;gt;
    &amp;lt;%= text_field_tag :model, :state %&amp;gt;
  &amp;lt;% else %&amp;gt;
    &amp;lt;%= select :model, :state, @states%&amp;gt;
  &amp;lt;% end %&amp;gt;
&amp;lt;/div&amp;gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/javascript/onchange-event-fired-from-select-field-in-rails-form/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
	</channel>
</rss>

