【发布时间】:2013-02-07 16:28:19
【问题描述】:
我在尝试 .build 新约会时遇到问题。但首先看看我的模型
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(通过 :appointments)]
程序模型 -> has_many [:appointments, :patients (through :appointments)]
预约模型 -> belongs_to [:patients, :procedures]
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
【问题讨论】:
-
如果您在 rspec 测试中遇到错误,为什么不显示该测试的代码?
-
失败的规范在第 16、23 和 35 行,但我猜
NoMethodError是因为你的lets 之一而被提出的。如果没有看到更多代码和更多堆栈跟踪,就无法判断,但我的猜测是在您的一个工厂中,您正在为一个零值的user或procedure变量做类似user.appointments或procedure.appointments的事情.不过,在将更多代码/堆栈跟踪粘贴到这篇文章之前,看看您是否可以自己定位问题(我认为发布大堆栈跟踪是最后的手段)。 -
不确定我是否理解其中的任何一个。但我调用的唯一 .appointments 变量是 patient.appointments
-
所以没有错误指向你的控制器或模型中的一行?开始为患者添加调试打印语句。听起来它没有被设置在某个地方,所以只需在调用
@patient.appointment之前添加一些puts "[DEBUG01] patient = '#{@patient}'"。 -
添加了整个 rspec 错误。它说我的约会控制器.rb 有问题。第 7 行反映了上面的情况,但由于我是 Rails 新手,所以对我来说确实是正确的。
标签: ruby ruby-on-rails-3.2 associations has-many-through nomethoderror