これが私が扱っていることですが、最初に何をする必要があるかについて少し背景を説明します。3 つのモデルがあります: 患者 -- 予約 -- 手順
これら 3 つのモデル内には、2 つのビューがあります。
これらの 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
以上が設定です。patients_controller で次の 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 のフォーム create メソッドに渡すには、現在の患者をどこで定義する必要がありますか?