Ruby Rails 3未定义的方法nil:NilClass在尝试创建时有\u many:through

Ruby Rails 3未定义的方法nil:NilClass在尝试创建时有\u many:through,ruby,ruby-on-rails-3.2,associations,has-many-through,nomethoderror,Ruby,Ruby On Rails 3.2,Associations,Has Many Through,Nomethoderror,我在尝试建立新约会时遇到问题。但首先看看我的模型 class Patient

我在尝试建立新约会时遇到问题。但首先看看我的模型

class Patient
类过程
班级预约
类指定控制器
模块PatientsHelper
def当前_患者=(患者)
@当前患者=患者
结束
当前患者
@现症病人
结束
def当前患者?(患者)
患者==当前患者
结束
结束
这些项目包括帮助定义当前患者的“PatientsHelper”组成关联。我已经成功地在patients_控制器中创建了表单

class PatientsController
我想使用患者资源创建新预约。这就是我被弄脏的地方。我继续通过rspec接收以下错误:

NoMethodError:
   undefined method `appointments' for nil:NilClass
我哪里做错了?任何帮助都将不胜感激。如果需要澄清,我想:

合伙人

患者模型->有许多[:预约,:程序(通过:预约)]

程序模型->有许多[:预约,:患者(通过:预约)]

预约模式->属于[:患者,:程序]

b_uu创建新:通过患者控制器进行预约,而不是专门为预约模型创建新控制器

我错了!这是规格测试

require'spec\u helper'
描述“约会页面”是什么
主题{page}
let(:user){FactoryGirl.create(:user)}
让(:病人){FactoryGirl.create(:病人)}
let(:过程){FactoryGirl.create(:过程)}
在{sign_in user}之前
请描述“创建约会”的方法
在{访问患者路径(患者)}之前
描述“使用无效信息”吗
它“不应该创建约会”吗
期待{点击按钮“日程安排程序”}。不改变(约会,
:计数)
结束
请描述“错误消息”的含义
在{单击按钮“计划程序”}之前
它{应该有_内容('error')}
结束
结束
请描述“使用有效信息”做什么
在做之前
procedure\u id=procedure.id
患者号=患者号
在“预约日期”中填写:“2013-04-04”
在“约会时间”中填写:“12:45:00”
在“约会笔记”中填写:“测试笔记”
结束
它“应该创造一个微成本”吗
期望{点击按钮“日程安排程序”}。更改(约会,
:计数)。按(1)
结束
结束
结束
结束
错误会显示在下面的页面上

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
故障:
1) 约会页面具有无效信息的约会创建不应创建约会
失败/错误:预计{单击按钮“计划程序”}。不更改(约会,
命名错误:
nil:NilClass的未定义方法“约会”
#./app/controllers/appointments\u controller.rb:7:in'create'
#(评估):2:在“单击按钮”中
#./spec/requests/appointment_pages_spec.rb:17:in'block(5级)in'
#./spec/requests/appointment_pages_spec.rb:17:in'block(4层)in'
2) 约会页面使用无效信息创建约会错误消息
失败/错误:在{单击按钮“计划程序”}之前
命名错误:
nil:NilClass的未定义方法“约会”
#./app/controllers/appointments\u controller.rb:7:in'create'
#(评估):2:在“单击按钮”中
#./spec/requests/appointment_pages_spec.rb:22:in'block(5级)in'
3) 约会页面具有有效信息的约会创建应创建一个micropost
失败/错误:希望{单击按钮“计划程序”}。更改(约会,
命名错误:
nil:NilClass的未定义方法“约会”
#./app/controllers/appointments\u controller.rb:7:in'create'
#(评估):2:在“单击按钮”中
#./spec/requests/appointment_pages_spec.rb:36:in'block(5级)in'
#./spec/requests/appointment_pages_spec.rb:36:in'block(4层)in'
以1.08秒完成
23例,3次失败
失败的示例:
rspec./spec/requests/appointment_pages_spec.rb:16#约会页面使用无效信息创建约会不应创建约会
rspec./spec/requests/appointment\u页面\u spec.rb:23
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