Ruby on rails 如何使用Jquery Ajax请求加载页面
我有一个索引页,可以加载当天预订的所有约会Ruby on rails 如何使用Jquery Ajax请求加载页面,ruby-on-rails,ruby,ajax,ruby-on-rails-4,Ruby On Rails,Ruby,Ajax,Ruby On Rails 4,我有一个索引页,可以加载当天预订的所有约会 def index @appointments = Appointment.where(appointment_date: Date.today, is_deleted: false) end 索引页中有一个日期字段,可用于选择日期 <%= date_field 'apppointment','booking_date' , class: 'appointment_booked_date' %> 在我的控制器里 def show_b
def index
@appointments = Appointment.where(appointment_date: Date.today, is_deleted: false)
end
索引页中有一个日期字段,可用于选择日期
<%= date_field 'apppointment','booking_date' , class: 'appointment_booked_date' %>
在我的控制器里
def show_bookings
@appointments = Appointment.where(appointment_date: params[:selected_date], is_deleted: false)
end
但是我不知道如何重新加载这个页面。这样我就可以更新我索引页面中的@Appoints值
请帮忙
但我不知道如何重新加载此页面
您不需要重新加载页面—您将返回对原始(Ajax)请求的响应。这可以通过代码块来处理:
#app/controllers/appointments_controller.rb
Class AppointmentsController < ApplicationController
def show_bookings
@appointment = ...
respond_to do |format|
format.html
format.js #-> loads views/appointments/show_bookings.js.erb
end
end
end
您还可以使用
respond\u with
。这是一种非常干燥的技术:
#app/controllers/appointments_controller.rb
Class AppointmentsController < ApplicationController
respond_to :js, :json, :html
def show_bookings
@appointments = ...
respond_with @appointments
end
end
您收到的“响应”取决于您使用的respond\u到
或respond\u块。任何一个都可以让您在应用程序中使用返回的数据
#app/views/appointments/show_bookings.js.erb
alert ("<%=j @appointments %>");
#app/controllers/appointments_controller.rb
Class AppointmentsController < ApplicationController
respond_to :js, :json, :html
def show_bookings
@appointments = ...
respond_with @appointments
end
end
$('#apppointment_booking_date').on('change', function() {
selected_date = this.value;
$.ajax({
url : '/appointments/show_bookings',
type : 'GET',
data : {
selected_date : selected_date
},
success : function(response) {
alert(response); #-> "console.log" doesn't work for us in Rails
}
});
});