I am new to ruby on rails and JQuery. Still i am in learning stage, In my development i want to display the data in show.html.erb using Jquery i need to fetch and display it when user click the button. I am using AJAX to get the data from the controller method i tried different way to achieve that but for me it is new to debug the issue. Can any one please help.
JQuery Code:
$(document).ready(function()
{
$('#patient_list').click(function()
{
var key = $("#totalPatient").val();
$('#patient').empty();
$("#patient").append("Sample");
$.ajax(
{
type: "GET",
url: "/show",
dataType: "json",
contentType: "application/json;charset=utf-8",
data: {'doctor_id': doc_id},
success: function(result)
{
for(term in result)
{
render = false;
$("#patient").append("<p>"+result[term].id"</p>");
}
},
error: function()
{
alert("something wrong
!");
}
})
});
Routes
resources :patient_details do
collection do
get 'show'
end
end
patient_details_controller.rb
class PatientDetailsController < ApplicationController
before_action :set_patient_detail, only: [:show, :edit, :update, :destroy]
respond_to :json, :html
def show
@patient_details = PatientDetail.where(Doctor_id: params[:doctor_id])
respond_to do |format|
format.html
format.json { render json: @patient_details } # respond with the created JSON object
end
end
end
show.json.jbuilder
json.array!(@patient_details) do |patient|
json.extract! patient, :id
end
show.html
<div class="table-responsive">
<table class ="table table-stripe">
<tbody>
<div class="form-group">
<button type="submit" name="patient_list" class="btn btn-info btn-block" id="patient_list"><h6><b>Click here to view the list of patient</b></h6></button>
<button type="submit" name="patient_list1" class="btn btn-info btn-block" id="patient_list1"><h6><b>Click here to disable the list of patient</b></h6></button>
<div id="patient"></div>
<input type="hidden" value="<%= @patient_details.length %>" id="totalPatient">
</div>
</tbody>
</table>
</div>
Any one please give me suggestions about a problem i am facing if possible explain me were i gone wrong with the above code.
Thanks in Advance
Aucun commentaire:
Enregistrer un commentaire