0
我試圖代碼的後續串另一個模板視圖:Rails的3.2.x中渲染特定ID
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。任何幫助將不勝感激。
這樣做效果很好。謝謝,但確實按照您的建議將變量設置爲「private」而不是「protected」。總而言之,這一切都很好!乾杯! – sandovalg 2013-02-25 08:11:06