title: Two collection select in form
body: <p>I have three models</p>
  
  <pre><code>City
  belongs_to :country
  
  Country
  has_many :cities
  
  User
  belongs_to :city
  </code></pre>
  
  <p>In the user form i want to make two select, the first is to select a country</p>
  
  <pre><code>&lt;%= f.collection_select :country_id, Country.all, :id, :name, options = {:prompt =&gt; 'Select a Country'}, html_options = {class:"form-control", required:true} %&gt;
  </code></pre>
  
  <p>That work fine for me. But the problem is to select a city, i don't know how i can list only the cities of the selected country. I read about grouped_collection_select but i can't resolve my problem.</p>
  
  <p>How can i do that? Thanks!</p>
  
  <p>P.D: I don't want to use a gem.</p>
  
  <hr>
  
  <p>I resolved the problem using grouped_collection_select</p>
  
  <pre><code>&lt;%= f.grouped_collection_select :city_id, Pais.order(:name),:cities,:name, :id, :name, options = {:prompt =&gt; 'Select a City'}, html_options = {class:"form-control", required:true}%&gt;
  </code></pre>
  
  <p>and jquery</p>
  
  <pre><code>$(function() {
      filterCitiesList();
  })
  function filterCitiesList() {
      $('#user_city_id').hide();
      users = $('#user_city_id').html();
      $('#user_country_id').change(function() {
          country_id = $('#user_country_id :selected').text();
          optgroup = "optgroup[label='" + country_id + "']"
          options = $(cities).filter(optgroup).html();
  
          if (country_id != "Select Country") {
              $('#user_city_id').show();
              $('#user_city_id').html(options);
          }
          else{
              $('#user_city_id').hide();
          }
      });
  }
  </code></pre>
  
  <p>Now i have another problem. I've modals windows, when i hide the Country modal i use </p>
  
  <pre><code>&lt;%= select_tag "countries",options_from_collection_for_select(Country.all, "id", "name"), {id:"user_country_id", name:"user[country_id]",include_blank: 'Select Country', class: "form-control"}%&gt;
  </code></pre>
  
  <h2>to render the country select. And the new question is How can i do that with grouped_collection_select?</h2>
  
  <p>I resolved the problem using option_groups_from_collection_for_select</p>
  
  <pre><code>&lt;%= select_tag "cities",option_groups_from_collection_for_select(Country.order(:name), :cities, :name, :id, :name), {id:"user_city_id", name:"user[city_id]",include_blank: 'Select City', class: "form-control"} %&gt;
  </code></pre>
  
  <p>Thanks for your answers!</p>