Question

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.

Was it helpful?

Solution

You most probable problem is you are using variables in patients/show that isn't declared in the create action when the validation fails and you render the template. The best way to fix this is to declare the same set of variables used on the show action on the create action if validation fails

def show
  @patients = ...
  @files = ...
end

def create
  if @object_that_fails_validation.save
  else
    @patient = ...
    @files = ...
    #render the show action
  end
end

if you feel this isn't DRY especially when you declare a lot of variables, either submit the form via ajax or move the variables to a different method

def show
  set_variables
end

def create
  if @object_that_fails_validation.save
  else
    set_variables
    #render the show action
  end
end

protected

def set_variable
  @patient = ...
  @files = ...
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top