title: Fill form text box when dropdown selected
body: <p>I have a form that is pulling data from multiple sources to intelligently prefill some defaults in the form. I would like to also prefill a "visit_price" text box that will be editable when the "visit_type" drop down is selected. This dropdown is a grouped_collection_select that is ajdusted via jquery by a Name search field (as per <a href="http://railscasts.com/episodes/88-dynamic-select-menus-revised" rel="nofollow">this</a> and <a href="http://railscasts.com/episodes/102-auto-complete-association-revised" rel="nofollow">this</a> railscast video)</p>
  
  <p>Below is the form view, model associations and coffeescript so far. I've also included the relevant html output.</p>
  
  <p><strong>edit:</strong>
  To be clear, I'm not sure how to get the price that is associated with the client price that is selected from the dropdown. </p>
  
  <p>Visit Block form View</p>
  
  <pre><code>&lt;%= form_for(@visit_block) do |f| %&gt;
  
    &lt;div class="controls controls-row"&gt;
      &lt;div class="form-inputs"&gt;
        &lt;%= f.text_field :client_name, data: {autocomplete_source: current_user.clients.order(:last_name).map(&amp;:last_name)} %&gt;
      &lt;/div&gt;
      &lt;div class="form-inputs"&gt;
        &lt;%= f.grouped_collection_select :visit_type, current_user.clients.order(:last_name), :client_prices, :last_name, :visit_type, :visit_type,  include_blank: true %&gt;
        &lt;%= f.text_field :visit_price %&gt;
      &lt;/div&gt;
      &lt;/div&gt;
    &lt;div class="form-actions"&gt;
      &lt;%= f.button :submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Models</p>
  
  <pre><code>class VisitBlock &lt; ActiveRecord::Base
    attr_accessible :visit_price, :visit_type, :client_id
  
    belongs_to :client
  
    def client_name
      client.try(:last_name)
    end
  
    def client_name=(last_name)
      self.client = Client.find_by_last_name(last_name) if last_name.present?
    end
  
  end
  
  
  class Client &lt; ActiveRecord::Base
    attr_accessible :active, :address_1, :address_2, :city, :email, :first_name, :last_name, :state, :user_id, :zip, :client_prices_attributes
    has_many :client_prices, :dependent =&gt; :destroy
  
  end
  
  class ClientPrice &lt; ActiveRecord::Base
    attr_accessible :client_id, :price, :visit_type, :id, :default_price_id, :custom, :_destroy
  
    belongs_to :client
    belongs_to :default_price
  
  
  end
  </code></pre>
  
  <p>Coffeescript (so far)</p>
  
  <pre><code>jQuery -&gt;
    $('#visit_block_client_name').autocomplete
      source: $('#visit_block_client_name').data('autocomplete-source')
  
    visit_types = $("#visit_block_visit_type").html()
    console.log visit_types
    $("#visit_block_client_name").keyup -&gt;
      client_name = visit_block_client_name.value
      options = $(visit_types).filter("optgroup[label=" + client_name + "]").html()
      console.log options
      if options
        $("#visit_block_visit_type").html options
      else
        $("#visit_block_visit_type").empty()
  
    $("#ui-id-1").click -&gt;
      client_name = visit_block_client_name.value
      options = $(visit_types).filter("optgroup[label=" + client_name + "]").html()
      console.log options
      if options
        $("#visit_block_visit_type").html options
      else
        $("#visit_block_visit_type").empty()
  </code></pre>
  
  <p>HTML output (so far)</p>
  
  <pre><code>              &lt;h1&gt;New visit_block&lt;/h1&gt;
  
  &lt;form accept-charset="UTF-8" action="/visit_blocks" class="new_visit_block" id="new_visit_block" method="post"&gt;&lt;div style="margin:0;padding:0;display:inline"&gt;&lt;input name="utf8" type="hidden" value="&amp;#x2713;" /&gt;&lt;input name="authenticity_token" type="hidden" value="I4xbbvdswv18TtP4LWoZHo7udmABT0J/nvKEPLFfO5Q=" /&gt;&lt;/div&gt;
  
    &lt;div class="controls controls-row"&gt;
      &lt;div class="form-inputs"&gt;
        &lt;input data-autocomplete-source="[&amp;quot;Client1&amp;quot;,&amp;quot;Client2&amp;quot;,&amp;quot;Client3&amp;quot;]" id="visit_block_client_name" name="visit_block[client_name]" size="30" type="text" /&gt;
      &lt;/div&gt;
      &lt;div class="form-inputs"&gt;
        &lt;select id="visit_block_visit_type" name="visit_block[visit_type]"&gt;&lt;option value=""&gt;&lt;/option&gt;
  &lt;optgroup label="Client1"&gt;
  &lt;option value="morning"&gt;morning&lt;/option&gt;
  &lt;option value="Morning weekday visit"&gt;Morning weekday visit&lt;/option&gt;
  &lt;/optgroup&gt;
  &lt;optgroup label="Client2"&gt;
  &lt;option value="evening"&gt;evening&lt;/option&gt;
  &lt;option value="Morning weekday visit"&gt;Morning weekday visit&lt;/option&gt;
  &lt;/optgroup&gt;
  &lt;optgroup label="Client3"&gt;
  &lt;option value="afternoon"&gt;afternoon&lt;/option&gt;
  &lt;option value="Morning weekday visit"&gt;Morning weekday visit&lt;/option&gt;
  &lt;/optgroup&gt;
  &lt;/select&gt;
        &lt;input id="visit_block_visit_price" name="visit_block[visit_price]" size="30" type="text" /&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-actions"&gt;
      &lt;button name="button" type="submit"&gt;submit&lt;/button&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>