Rails:未执行检查数据库约会的方法 - 不保存

时间:2016-06-30 17:09:37

标签: ruby-on-rails

我有一个非常初学者对铁轨和红宝石的理解,我保留 卡住了。如果有人能指出我出错的地方,那就太好了!否则,在允许预约之前,是否有更简单的方法来验证数据库?我不想要双重预订。

我正在预约预约应用程序,我有一个非常基本的设计。一世 已经创建了一个名为的约会脚手架:string phone:string email:string numpeople:integer date:date timeslot:string。 在创建新约会的观点中,我已经说过约会1是 上午9点到11点,预约2点是下午12点到2点,预约3点是下午3-5点并预约 4是下午5点到7点。要求用户输入1,2,3或4。

当用户点击“预约”时,我试图打断 约会控制器(创建方法),以便我可以检查日期 &安培;&安培;时间段是零。如果是这种情况,系统应该继续 创建约会,如果没有,那么我想重定向到某个地方 其他。我创建了一个名为isValid的方法?在模型中(见下文)

我认为这个方法是正确的,因为系统已经达到了 重定向。 Tclass约会<的ActiveRecord ::基

class Appointment < ActiveRecord::Base

   def isValid?
      taken = Appointment.where("date = ? && timeslot = ?", date, timeslot)
      save unless taken
end
end

问题是,它一直重定向到我告诉它的页面 转到它是否未保存(主页或root_path)。 (也是 约会没有保存。)

约会控制器创建方法:

def create
  valid = @appointment = Appointment.new(appointment_params).isValid?

  respond_to do |format|
  if valid
    format.html { redirect_to new_appointment_path, notice: 'Appointment was 
    successfully created.' }
    format.json { render :show, status: :created, location: @appointment }
  else
    format.html { redirect_to appointments_path, notice: 'That appointment 
    is not available, please choose again' } # this redirect works with no 
    notice
    format.js { render json: @appointment.errors, status: 
    :unprocessable_entity }
  end
 end
end

完全约会控制器类:(如果我错过了什么)

 class AppointmentsController < ApplicationController
 before_action :set_appointment, only: [:show, :edit, :update, :destroy]

def index
   @appointments = Appointment.all
end

def show
end

def new
   @appointment = Appointment.new
end

def edit
end

def create
  valid = @appointment = Appointment.new(appointment_params).isValid?

  respond_to do |format|
  if valid
    format.html { redirect_to new_appointment_path, notice: 'Appointment was 
    successfully created.' }
    format.json { render :show, status: :created, location: @appointment }
  else
    format.html { redirect_to appointments_path, notice: 'That appointment 
    is not available, please choose again' } # this redirect works with no 
    notice
    format.js { render json: @appointment.errors, status: 
    :unprocessable_entity }
  end
end
end

def update
   respond_to do |format|
   if @appointment.update(appointment_params)
      format.html { redirect_to @appointment, notice: 'Appointment was 
      successfully updated.' }
      format.json { render :show, status: :ok, location: @appointment }
   else
      format.html { render :edit }
      format.json { render json: @appointment.errors, status: 
      :unprocessable_entity }
   end
  end
  end

def destroy
   @appointment.destroy
     respond_to do |format|
     format.html { redirect_to appointments_url, notice: 'Appointment was 
     successfully destroyed.' }
     format.json { head :no_content }
 end
end

private
# Use callbacks to share common setup or constraints between actions.
  def set_appointment
     @appointment = Appointment.find(params[:id])
  end

# Never trust parameters from the scary internet, only allow the white list 
through.

def appointment_params
  params.require(:appointment).permit(:name, :phone, :email, :numpeople, 
  :date, :timeslot)
end
end

1 个答案:

答案 0 :(得分:0)

我认为你的方法应该是这样的:

class Appointment < ActiveRecord::Base

   def isValid?
      taken = Appointment.where("date = ? && timeslot = ?", date, timeslot)
      save unless taken.present?
   end
end

如果有效,请告诉我。