Reservation Flashcards
1Basic:
Reservation Scaffold
rails g scaffold User: references Room: references Start_date: datetime End_date: datetime Price: Integer Total: Integer
1Basic Reservation:
routes.rb
resources :room do
resources: reservations, only: [create]
end
1Basic:
reservations_controller.rb
Class ReservartionsController
1Basic:
_form.html.erb
//####Basic _form
<span></span> <div style="display: none;"> <span> <span></span>
</span></div>
2JQuery:
Gemfile
gem ‘jquery-ui-rails’
2JQuery:
application.js
//= require jquery-ui/datepicker
2JQuery:
JavaScript
_form.html.erb
$(function(){
$(‘#reservation_start_date’).datepicker({ dateFormat: ‘dd-mm-yy’ });
$(‘#reservation_end_date’).datepicker({ dateFormat: ‘dd-mm-yy’ });
}); });
2JQuery:
_form.html.erb
//####Basic _form
<span></span> <div style="display: none;"> <span> <span></span>
</span></div>
3AJAX: Start_date
Reservation_Controller.rb
class ReservationsController = ? OR end_date >= ?, today, today) render json: reservations end … end
3AJAX: Start_date
routes.rb
get ‘/preload’=> ‘reservations#preload’
3AJAX:
Start_date
_form.html.erb
Part 1 ( Unavailable function)
function unavailable(date){ dmy= date.getDate() +"-"+ (date.getMonth()+1)+"-" + date.getFullYear(); return [$.inArray(dmy, unavailableDates) == -1 ] } }
3AJAX:
Start_date
_form.html.erb
Part 2: AJAX working the DOM
$(function(){ unavailableDates = []; $.ajax({ url: '/preload', data: {'room_id': }, dataType: 'json', success: function(data){ $.each(data, function(arrID, arrValue){ for(var d = new Date(arrValue.start_date); d
3AJAX:
Start_date
_form.html.erb
Part 3: AJAX reservation_start_date
$(‘#reservation_start_date’).datepicker({
dateFormat: ‘dd-mm-yy’,
minDate: 0,
maxDate: ‘3m’,
beforeShowDay: unavailable ,
onSelect: function(selected) {
$(‘#reservation_end_date’).datepicker(“option”, “minDate”, selected);
$(‘#reservation_end_date’).attr(‘disabled’, false);
} });
3AJAX:
Start_date
_form.html.erb
Part 4: AJAX reservation_end_date
$('#reservation_end_date').datepicker({ dateFormat: 'dd-mm-yy', minDate: 0, maxDate: '3m', beforeShowDay: unavailable }); onSelect: function(selected) { $('#reservation_start_date').datepicker("option", "maxDate", selected); } });
4AJAX:
End_date (1/2)
reservations_controller.rb
Class ReservartionsController = ? OR end_date >= ?, today, today)
render json: reservations
end
def preview
start_date = Date.parse(params[:start_date])
end_date = Date.parse(params[:end_date])
output = { conflict: is_conflict(start_date, end_date) }
render json: output
end
def create
@reservation = current_user.reservations.create(reservation_params)
redirect_to @reservation.room, notice: “Your reservations have been made…”
end