我正在尝试以下代码字符串:
class AppointmentsController < ApplicationController
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
目前正在通过三个控制器。其中两个似乎很重要。
class Appointment < ActiveRecord::Base
attr_accessible :appointment_date, :appointment_notes, :appointment_time, :procedure_id, :patient_id
belongs_to :patient
belongs_to :procedure
validates :procedure_id, presence: true
validates :patient_id, presence: true
validates :appointment_date, presence: true
validates :appointment_time, presence: true
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
我的创建方法效果很好。但是,当我提交数据并且未在约会中通过验证时,它应该呈现正确的 app.dev/patients/:id 页面,其中 :id 是我正在使用的当前页面。有问题的表格是创建约会的表格(通过患者/显示视图)。当提交不正确或零数据并且需要存在:true 时,我希望呈现相同的页面。我目前收到的是:
rspec
ActionView::Template::Error:
undefined method `first_name' for nil:NilClass
# ./app/views/patients/show.html.erb:1:in `_app_views_patients_show_html_erb__4137167421928365638_70201005779320'
# ./app/controllers/appointments_controller.rb:11:in `create'
我怀疑这与能够在正确的路径上相应地设置渲染有关,特别是调用正确的患者/ID。任何帮助将不胜感激。