我正在使用grouped_select
进行预订,其中可以根据空房情况选择房间。每次用户更改arrival
和/或departure
的值时,rooms
(属于room_types
)都会根据该时间段的可用性进行调整。>
当我尝试编辑预订时,如果我不调整到达或离开的日期,一切都会很好(例如,先前选择的room
并因此将其room_type
显示为默认)。
问题
当我更改arrrival
或departure
的日期时,我会重新输入可用的房间,并且先前选择的room
(及其room_category
)不见了如果该空间可供新选择的room
和arrival
日期使用,是否有办法将先前选择的departure
显示为默认值?
代码
预订表格
<div class="col col-sm-4">
<%= f.input :arrival,as: :string,label:false,placeholder: "From",wrapper_html: { class: "inline_field_wrapper" },input_html:{ id: "start_date"} %>
</div>
<div class="col col-sm-4">
<%= f.input :departure,input_html:{ id: "end_date"} %>
</div>
<div class="col col-sm-4">
<%= f.input :room_id,collection: @room_categories.order(:name),as: :grouped_select,group_method: :rooms,label:false %>
</div>
JS
const checkIn = document.querySelector('#start_date');
const checkOut = document.querySelector('#end_date');
const checkInAndOut = [checkIn,checkOut];
checkInAndOut.forEach((item) => {
item.addEventListener('change',(event) => {
if ((checkIn.value.length > 0) && (checkOut.value.length > 0)){
checkAvailability();
}
})
});
function checkAvailability(){
Rails.ajax({
url: "<%= rooms_availability_hotel_path(@hotel) %>",dataType: 'json',type: "POST",data: `arrival=${start_date.value}&departure=${end_date.value}`,success: function(data) {
console.log(data);
},error: function(response) {
console.log(response);
}
});
};
控制器检查可用性
def rooms_availability
hotel = Hotel.includes(:rooms).find(params[:id])
arrival = Date.parse(room_params[:arrival])
departure = Date.parse(room_params[:departure])
time_span = arrival..departure
@unavailable_rooms = Room.joins(:reservations).where(reservations: {hotel: hotel}).where("reservations.arrival <= ? AND ? >= reservations.departure",arrival,departure).distinct + (Room.joins(:reservations).where(reservations: {hotel: hotel}).where("reservations.arrival <= ? AND ? <= reservations.departure",departure).distinct)
@hotel_cats = hotel.room_categories
@hotel_accos = Room.where(room_category: @hotel_cats)
@rooms = @hotel_accos - @unavailable_rooms
respond_to do |format|
format.js
end
end