0

这是我正在处理的内容,但首先需要了解需要做什么的背景知识。有 3 种模型:患者 -- 预约 -- 程序

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

在这 2 个视图中,我想通过患者(显示)视图安排约会。这实质上将为视图中的患者(特别是视图中的患者 ID)创建一个新约会。

这是模型的代码

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

这就是设置,我在患者控制器中收到以下 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 的表单创建方法?

4

1 回答 1

0

不完全是我想要的,但解决了这个问题。

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

  1. 在表单中,我添加了一个隐藏字段,用于提取适当的患者 ID
  2. 在 Appointments 控制器中,添加到 attr_accessible :patient_id
  3. 在约会_控制器

    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'仍然损坏,但会将其留给另一个线程。谢谢大家的帮助和指导。同样,我怀疑渲染调用与当前困扰我从约会控制器模型直接访问 current_patient 的问题有关。

于 2013-02-24T07:20:19.427 回答