0

これが私が扱っていることですが、最初に何をする必要があるかについて少し背景を説明します。3 つのモデルがあります: 患者 -- 予約 -- 手順

これら 3 つのモデル内には、2 つのビューがあります。

これらの 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

以上が設定です。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 のフォーム create メソッドに渡すには、現在の患者をどこで定義する必要がありますか?

4

1 に答える 1

0

まさに私が探していたものではありませんが、この回避策を作成しました。

以下を使用すると、これを適切に保存することができました。URLから患者を取得できない理由をまだ見つけることができません.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'壊れたままですが、別のスレッドに残します。皆様のご支援とご指導をよろしくお願いいたします。繰り返しますが、render 呼び出しは、現在、アポイントメント コントローラー モデルから直接 current_patient にアクセスすることを悩ませている同じ問題に関係していると思われます。

于 2013-02-24T07:20:19.427 に答える