这是我正在处理的内容,但首先需要了解需要做什么的背景知识。有 3 种模型:患者 -- 预约 -- 程序
在这 3 个模型中,有两个视图程序——患者
在这 2 个视图中,我想通过患者(显示)视图安排约会。这实质上将为视图中的患者(特别是视图中的患者 ID)创建一个新约会。
这是模型的代码
class Patient < ActiveRecord::Base
attr_accessible :address1, :address2, :city, :comment, :email, :first_name, :init_date, :init_time, :last_name, :mobile, :notes, :phone, :state, :zip
has_many :appointments, dependent: :destroy
has_many :procedures, through: :appointments
class Procedure < ActiveRecord::Base
attr_accessible :comment, :occurence, :procedure, :procedure_code, :procedure_price, :procedure_time, :visits
has_many :appointments
has_many :patients, through: :appointments
class Appointment < ActiveRecord::Base
attr_accessible :appointment_date, :appointment_notes, :appointment_time, :procedure_id
belongs_to :patient
belongs_to :procedure
这是约会的控制器以及包含约会的 routes.rb (但只是那一行)
resources :appointments, only: [:create, :destroy, :edit, :update]
class AppointmentsController < ApplicationController
include PatientsHelper
before_filter :signed_in_user
def create
@current_patient = @patient.id
@appointment = @current_patient.appointments.build(params[:appointment])
if @appointment.save
flash[:success] = "Appointment scheduled!"
redirect_to patient_path(@current_patient)
else
render 'create'
end
end
module PatientsHelper
def current_patient=(patient)
@current_patient = patient
end
def current_patient
@current_patient
end
def current_patient?(patient)
patient == current_patient
end
end
这就是设置,我在患者控制器中收到以下 rspec 错误。通过首先为@current_patient = Patient.first 定义一个ID,在控制台中测试了@appointment = @current_patient.appointments.build(params[:appointment])。这行得通,并且构建会按应有的方式进行。错误:
失败:
1) Appointment Pages appointment creation with invalid information should not create an appointment
Failure/Error: expect { click_button "Schedule procedure" }.not_to change(Appointment,
RuntimeError:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
# ./app/controllers/appointments_controller.rb:6:in `create'
# (eval):2:in `click_button'
# ./spec/requests/appointment_pages_spec.rb:17:in `block (5 levels) in <top (required)>'
# ./spec/requests/appointment_pages_spec.rb:17:in `block (4 levels) in <top (required)>'
2) Appointment Pages appointment creation with invalid information error messages
Failure/Error: before { click_button "Schedule procedure" }
RuntimeError:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
# ./app/controllers/appointments_controller.rb:6:in `create'
# (eval):2:in `click_button'
# ./spec/requests/appointment_pages_spec.rb:22:in `block (5 levels) in <top (required)>'
3) Appointment Pages appointment creation with valid information should create a micropost
Failure/Error: expect { click_button "Schedule procedure" }.to change(Appointment,
RuntimeError:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
# ./app/controllers/appointments_controller.rb:6:in `create'
# (eval):2:in `click_button'
# ./spec/requests/appointment_pages_spec.rb:36:in `block (5 levels) in <top (required)>'
# ./spec/requests/appointment_pages_spec.rb:36:in `block (4 levels) in <top (required)>'
似乎我没有正确定义@current_patient = @patient.id... 或者更确切地说,当前的患者没有延续/通过预约表格。我需要在哪里定义当前患者以将该 ID 传递到 @current_patient.appointments.build 的表单创建方法?