jquery - Update Rails Variable in ERB using Ajax -
i have dropdown menu of patients. when user selects patient dropdown menu, want use jquery patient's id, make ajax call server return patient variable can use in view.
dropdown:
<%= f.select :patient_id, content_tag(:option,'select patient...',:value=>"")+content_tag(:option,'new patient',:value=>"")+options_from_collection_for_select(@patients, 'id', 'full_name'), :class=>"form-control" %>
js:
$('#assessment_patient_id').change(function(){ var e = document.getelementbyid("assessment_patient_id"); var patient_id = (e.options[e.selectedindex].value) if (patient_id == 0) { window.location.href = "/patients/new" } else { $.ajax({ type: "post", url: "get-patient", data: {patient_id: patient_id}, success: function(data){ console.log(data) }, error: function(err){ console.log(err); } }); }
controller:
respond_to :js, :html, :json def get_patient @patient = patient.find(params[:patient_id]) respond_with @patient end
routes:
'assessments/get-patient', :to => 'assessment#get_patient'
get-patient.js.erb
$('#assessment-type').slidedown(350);
what i'm trying accomplish ability go <%= @patient.method_name_here %> in view after user selects patient drop down.
but can't figure out how data object controller. go?
you this. in view add div id#patient-name (for example)
then, js.erb file can update dom.
# get-patient.js.erb $('#assessment-type').slidedown(350); $('#patient-name').html('#{j(@patient.name)}');
and, request go patients_controller#show instead?
Comments
Post a Comment