I have a form with two fields phone
and name
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.
Watching that Railscasts episode i was able to make ajax autocomplete works, any clean way of doing the auto filling?
patients_controller
class PatientsController < ApplicationController
def index
@patients = Patient.order(:phone).where('phone like ?', "%#{params[:term]}%")
json_data = @patients.map(&:phone)
render json: json_data
end
end
reservations.coffee
$(document).ready ->
$('#patient_phone').autocomplete
source: "/patients/index"
_form.erb
<%= f.fields_for :patient, appointment.patient do |patient| %>
<%= patient.text_field :phone, id: 'patient_phone' %>
<%= patient.text_field :name %>
<%= f.submit %>
<% end %>