Dynamic forms - Auto fill fields based on previous auto complete
<p>I have a form with two fields <code>phone</code> and <code>name</code> what i want is when the user selects a phone number using ajax autocomplete the name field will be auto filled with the name that correspond to that phone number.</p>

<p>Watching that <a href="https://www.youtube.com/watch?v=M7yhPlIehFA" rel="nofollow">Railscasts episode</a> i was able to make ajax autocomplete works, any clean way of doing the auto filling?</p>

<p>patients_controller</p>

<pre><code>class PatientsController &lt; ApplicationController
  def index
   @patients = Patient.order(:phone).where('phone like ?', "%#{params[:term]}%")
   json_data = @patients.map(&amp;:phone)
   render json: json_data
  end
end
</code></pre>

<p>reservations.coffee</p>

<pre><code>$(document).ready -&gt;
  $('#patient_phone').autocomplete
  source: "/patients/index"
</code></pre>

<p>_form.erb</p>

<pre><code>&lt;%= f.fields_for :patient, appointment.patient do |patient| %&gt;
  &lt;%= patient.text_field :phone, id: 'patient_phone' %&gt;
  &lt;%= patient.text_field :name %&gt;
  &lt;%= f.submit %&gt;
&lt;% end %&gt;
</code></pre>