防止在嵌套属性窗体中创建空白关联



我有一个带有嵌套属性的表单,该表单正在发送到约会控制器,该控制器正在创建称为处方,疾病,过敏和免疫的关联模型的实例。这个想法是医生可以填写表格,并可以在需要时添加信息,但不需要填写所有内容(并非每次预约都需要处方)。

目前,表单发出并创建所有关联的新实例,所有属性均为空白或 nil。我尝试添加需要存在要保存的属性的验证,但这会产生错误,然后无法保存任何内容。

如果该模型的字段为空,如何提交一个表单并防止它创建关联模型的实例?

约会控制器

def create
@appointment = current_user.appointments.new(appointment_params)
set_associations(@appointment, @patient)
if @appointment.save
Notification.create_appointment_notifications(@appointment, @health_profile)
flash[:notice] = "Your appointment has been saved."
redirect_to patient_health_profile_path(@patient, @health_profile)
else
flash[:notice] = "There was a problem saving your appointment, please try again."
redirect_to patient_health_profile_path(@patient, @health_profile)
end 

end
private
def appointment_params
symptoms = params[:appointment][:symptoms].split(",")
@patient = Patient.find(params[:patient_id])
params.require(:appointment).permit(
:diagnosis, :referrals, :notes, :symptoms,
immunizations_attributes: [:name, :expiration_date],
illnesses_attributes: [:name, :status],
allergies_attributes: [:name, :status, :severity],
prescriptions_attributes: [:medicine, :dosage, :refills, :expiration_date]
).merge(symptoms: symptoms,
patient: @patient
)
end
def set_associations(appointment, patient)
appointment.illnesses.each do |illness|
illness.patient = patient
end
appointment.allergies.each do |allergy|
allergy.patient = patient
end
appointment.immunizations.each do |immunization|
immunization.patient = patient
end
appointment.prescriptions.each do |prescription|
prescription.patient = patient
prescription.doctor = current_user
end
end
def set_information
@patient = Patient.find(params[:patient_id])
@health_profile = HealthProfile.find(params[:id])
end 

嵌套表单

<%= form_for @appointment do |f| %>
<div>
<h4>Appointment</h4>  
<div>
<%= f.label :Symptoms %>
<%= f.text_field :symptoms, id: "symptoms-input" %>
</div>
<div>
<%= f.label :Notes %>
<%= f.text_area :notes %>
</div>
<div>
<%= f.label :Diagnosis %>
<%= f.text_field :diagnosis %>
</div>
<div>
<%= f.label :Referrals %>
<%= f.text_area :referrals, id: "referrals-input" %>
</div>
</div>
<div>
<h4>Illnesses</h4>
<div>
<%= f.fields_for :illnesses do |illness| %>
<%= render "appointments/illness_fields", f: illness %>
<% end %>
</div>
<div>
<%= link_to_add_association '+ Illness', f, :illnesses, partial: "appointments/illness_fields" %>
</div>
</div>
<br>
<div>
<h4>Allergies</h4>
<div>
<%= f.fields_for :allergies do |allergy| %>
<%= render "appointments/allergy_fields", f: allergy %>
<% end %>
</div>
<div>
<%= link_to_add_association '+ Allergy', f, :allergies, partial: "appointments/allergy_fields" %>
</div>
</div>
<br>
<div>
<h4>Immunizations</h4>
<div>
<div>
<%= f.fields_for :immunizations do |immunization| %>
<%= render "appointments/immunization_fields", f: immunization %>
<% end%>
</div>
<div>
<%= link_to_add_association '+ Immunization', f, :immunizations, partial: "appointments/immunization_fields" %>
</div>
</div>
</div>
<br>
<div>
<h4>Prescriptions</h4>
<div>
<%= f.fields_for :prescriptions do |prescription| %>
<%= render "appointments/prescription_fields", f: prescription %>
<% end %>
</div>
<div>
<%= link_to_add_association '+ Prescription', f, :prescriptions, partial: "appointments/prescription_fields" %>
</div>
</div>
<%= hidden_field_tag(:patient_id, params[:patient_id]) %>
<%= hidden_field_tag(:id, params[:id]) %>
<div>
<%= f.submit :Submit %>
</div>
<% end %>

在约会模型中,添加以下行:

accepts_nested_attributes_for :illnesses, reject_if: :all_blank, allow_destroy: true

最新更新