title: Custom country/state dropdowns with rails
body: <p>I been diggins some days on gems for country and state/province selection. Some are great (like <a href="https://github.com/arvindvyas/Country-State-Select" rel="nofollow">country-state-select</a>) but not for my needs.</p>
  
  <p>The almost mandatory <a href="https://github.com/stefanpenner/country_select" rel="nofollow">country_select</a> gem is good but lacks states. Nevertheless is based on this very cool gem called <a href="https://github.com/hexorx/countries" rel="nofollow">countries</a>. Gem countries really puts together a lot of good info, lets say <a href="https://github.com/hexorx/countries/blob/master/lib/countries/data/countries/MZ.yaml" rel="nofollow">Mozambique</a> and its <a href="https://github.com/hexorx/countries/blob/master/lib/countries/data/subdivisions/MZ.yaml" rel="nofollow">subdivisions</a>, and so on, for over 300 countries, and includes i18n.</p>
  
  <p>Having countries gem in the app, all is needed is some javascript to interact with it. Calls are made with this:</p>
  
  <pre><code>country = ISO3166::Country.new('US')
  </code></pre>
  
  <p>Here is the form:</p>
  
  <pre><code>&lt;%= simple_form_for(@order) do |f| %&gt;
  &lt;div&gt;&lt;%= f.input :country %&gt;&lt;/div&gt;
  &lt;div&gt;&lt;%= f.input :state %&gt;&lt;/div&gt;
  &lt;%= f.button :submit, t('.submit') %&gt;
  &lt;% end %&gt;
  
  &lt;script type="text/javascript"&gt;
  states_drop_down = $("#order_state");
  $("#order_country").change(function() {
  
  // How to make this work?
      &lt;% unless params[:country].nil? %&gt;
          states_drop_down.clearAttributes();
          &lt;% ISO3166::Country.new(params[:country]).states.each do |state| %&gt;
              states_drop_down.setAttribute(&lt;%= state[:name] %&gt;, &lt;%= state[:alpha2] %&gt;); // How to log to check if new attributes are present?
          &lt;% end %&gt;
          states_drop_down.reload(); // How to reload the state simple_form input?
      &lt;% end %&gt;
  });
  </code></pre>
  
  <p></p>
  
  <p>The goal is the known one, to populate state selector with correct country every time the country dropdown changes. Any help? Thanks.</p>