Rails 3.2.x has_many,通过(3个模型)在创建之前调用nil id

时间:2013-02-23 19:53:59

标签: ruby-on-rails ruby-on-rails-3.2 rspec2 nested-attributes has-many-through

这是我正在处理的问题,但首先是关于需要做什么的一些背景知识。有3种模型:患者 - 预约 - 程序

在这3个模型中,有两个视图程序 - 患者

在这两个观点中,我想通过患者(展示)视图安排预约。实质上,这将为患者创建一个新的预约(特别是患者。在视图中)。

以下是模型的代码

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的表单创建方法?

1 个答案:

答案 0 :(得分:0)

不完全是我正在寻找的东西,但这使得这个解决方案。

使用以下内容可以相应地保存。 仍然无法找出为什么我无法从URL中拉出患者 - 例如app.dev/patients/2 - 其中患者信息正在提取ID ...但现在我是能够使用我怀疑的方法保存约会是一种相当粗略的方法。

  1. 在表单中,我添加了一个隐藏字段,用于拉出相应的patient_id
  2. 在Appointments控制器中,添加到attr_accessible:patient_id
  3. 在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
    
  4. render 'patients/show'仍然被打破,但会留给另一个线程。谢谢大家的帮助和指导。同样,我怀疑渲染调用与当前困扰我从appointmentments_controller模型直接访问current_patient的同一问题有关。