新しい予定を作成しようとすると問題が発生します。しかし、最初に私のモデルを見てください
class Patient < ActiveRecord::Base
attr_accessible :address1, :address2, :city, :comment, :email, :first_name, :init_date, :init_time, :last_name, :mobile, :notes, :phone, :state, :zip
before_validation :upcase_patient
before_save { self.email.downcase! }
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
before_validation :uppercase_procedure
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
class AppointmentsController < ApplicationController
include PatientsHelper
before_filter :signed_in_user
def create
@patient = current_patient
@appointment = @patient.appointments.build(params[:appointment])
if @appointment.save
flash[:success] = "Appointment scheduled!"
redirect_to patient_path(@patient)
else
render patient_path(@patient)
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
current_patientの定義に役立つ「PatientsHelper」を含むこれらのアイテムは、関連付けを構成します。私はpatients_controller内でフォームを正常に作成しました
class PatientsController < ApplicationController
before_filter :signed_in_user
before_filter :admin_user, only: [:destroy]
def show
@patient = Patient.find(params[:id])
@procedures = Procedure.all
@appointments = @patient.appointments.paginate(page: params[:page])
@appointment = @patient.appointments.build
end
患者のリソースを使用して、新しい予定を作成したいと思います。ここが私が汚されているところです。rspecを介して次のエラーを引き続き受け取ります。
NoMethodError:
undefined method `appointments' for nil:NilClass
どこが間違っているのですか?どんな助けでも大歓迎です。説明が必要な場合は、次のことを行います。
a_アソシエイト
患者モデル->has_many[:appointments、:procedures(through:appointments)]
プロシージャモデル->has_many[:appointments、:patients(through:appointments)]
予定モデル->belongs_to[:patients、:procedures]
b_予約モデル専用の新しいコントローラーを作成するのではなく、患者コントローラーを介して新しい:appointmentsを作成します
私の間違い!これがスペックテストです。
require 'spec_helper'
describe "Appointment Pages" do
subject { page }
let(:user) { FactoryGirl.create(:user) }
let(:patient) { FactoryGirl.create(:patient) }
let(:procedure) { FactoryGirl.create(:procedure) }
before { sign_in user }
describe "appointment creation" do
before { visit patient_path(patient) }
describe "with invalid information" do
it "should not create an appointment" do
expect { click_button "Schedule procedure" }.not_to change(Appointment,
:count)
end
describe "error messages" do
before { click_button "Schedule procedure" }
it { should have_content('error') }
end
end
describe "with valid information" do
before do
procedure_id = procedure.id
patient_id = patient.id
fill_in 'appointment_appointment_date', with: "2013-04-04"
fill_in 'appointment_appointment_time', with: "12:45:00"
fill_in 'appointment_appointment_notes', with: "Test the notes"
end
it "should create a micropost" do
expect { click_button "Schedule procedure" }.to change(Appointment,
:count).by(1)
end
end
end
end
エラーは次のように表示されます
Failures:
1) Appointment Pages appointment creation with invalid information should not create an appointment
Failure/Error: expect { click_button "Schedule procedure" }.not_to change(Appointment,
NoMethodError:
undefined method `appointments' for nil:NilClass
# ./app/controllers/appointments_controller.rb:7: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" }
NoMethodError:
undefined method `appointments' for nil:NilClass
# ./app/controllers/appointments_controller.rb:7: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,
NoMethodError:
undefined method `appointments' for nil:NilClass
# ./app/controllers/appointments_controller.rb:7: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)>'
Finished in 1.08 seconds
23 examples, 3 failures
Failed examples:
rspec ./spec/requests/appointment_pages_spec.rb:16 # Appointment Pages appointment creation with invalid information should not create an appointment
rspec ./spec/requests/appointment_pages_spec.rb:23 # Appointment Pages appointment creation with invalid information error messages
rspec ./spec/requests/appointment_pages_spec.rb:35 # Appointment Pages appointment creation with valid information should create a micropost