无法检查日期时间是否存在冲突

时间:2017-06-24 11:51:07

标签: ruby-on-rails ajax time conflict

我有一个按小时预订的rails应用程序。我想在预订前检查时间冲突。

DB

create_table :bookings do |t|
  t.references :user, foreign_key: true
  t.references :service, foreign_key: true
  t.datetime :booking_time
  t.datetime :booking_end

  t.integer :duration,  limit: 4
  t.integer :price
  t.integer :total
  t.boolean :status
  t.string :location, limit: 255

在控制器中

def create
    booking_time = Date.parse(params[:booking_time])
    duration = params[:duration]
    booking_params[:booking_end] = booking_time + duration.hour
    @booking = current_user.bookings.create(booking_params) end
private
    def is_conflict(booking_time, new_booking_end)
service = Service.find(params[:service_id])
check1 = service.bookings.where(" ? >= booking_time AND ? <= booking_end", booking_time, booking_time)
check2 = service.bookings.where(" ? >= booking_time AND ? <= booking_end", new_booking_end, new_booking_end)

check1.size > 0 || check2.size > 0

在视图中我有HTML和DATE / TIME选择器和ajax来显示预订btn如果没有冲突,否则显示日期不可用。 观点:

<label>Day</label>
    <%= f.text_field :booking_time, readonly: 'true', placeholder: 'start date', class: 'form-control' %>
</div>
    <div class="col-md-4">
    <label>Time</label>
    <%= f.text_field :booking_time, readonly: 'true', class: 'timepicker' %>
    </div>
    <div class="col-md-6">
    <label for=" check out "></label>
    <%= f.select :duration, [["1",1], ["2",2], ["3",3], ["4",4]], prompt: "Duration...", readonly: 'true', class: "form-control" %>

和Ajax函数:

var input = {
                    'booking_time': booking_time,
                        'duration': duration,
                        'service_id': <%= @service.id %>,
                    };



                    $.ajax({
                        url: "/preview",
                        data: input,
                        success: function(data) {

                            if (data.conflict) {

                                $('#message').text("This date range is not available.");
                                $('#preview').hide();
                                $('#btn_book').attr('disabled', true);

                            } else {

                                $('#preview').show();
                                $('#btn_book').attr('disabled', false);

                                var total = duration * <%= @service.price %>
                                $('#booking_hours').text(duration);
                                $('#booking_sum').text(total);
                                $('#booking_total').val(total);
                            }

当我尝试使用它时,我收到此错误the error

当我在def(创建)的第一行删除3行时,它可以工作,但不会将预订结束时间存储在DB中。

任何建议 感谢

0 个答案:

没有答案