I am attempting the follow string of code:
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
This is going through three controllers presently. Two of which seem to be of importance.
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
My create method works wonderful. However when I submit data and do not pass the validates in appointments it should render the correct app.dev/patients/:id page where :id is the current page I am working with. The form in question is the one that creates an appointment (through the Patients/show view). When incorrect or nil data is submitted and presence: true is required I would like the same page to render. What I currently receive is:
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'
I suspect this has to do with being able to accordingly set the render on the right path, specifically call the right patient/id. Any help would be greatly appreciated.