我在尝试。建立新约会时遇到问题。但首先看看我的模型
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_ associate
患者模型 - &gt; has_many [:约会,:程序(通过:约会)]
程序模型 - &gt; has_many [:约会,:患者(通过:约会)]
约会模型 - &gt; belongs_to [:病人,:程序]
b_创建新的:通过患者控制器进行预约,而不是专门为约会模型创建一个新的控制器
我错了!这是规范测试。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
答案 0 :(得分:0)
不完全是我正在寻找的东西,但这使得这个解决方案。
使用以下内容可以相应地保存。 仍然无法找出为什么我无法从URL中拉出患者 - 例如app.dev/patients/2 - 其中患者信息正在提取ID ...但现在我是能够使用我怀疑的方法保存约会是一种相当粗略的方法。
在appointmentments_controller中
def create
@appointment = Appointment.new(params[:appointment])
@current_patient = @appointment.patient_id
if @appointment.save
flash[:success] = "Appointment scheduled!"
redirect_to patient_path(@current_patient)
else
render 'patients/show'
end
end
render 'patients/show'
仍然被打破,但会留给另一个线程。谢谢大家的帮助和指导。