简体   繁体   English

如何在没有nested_form的情况下将表单写入has_many关联?

[英]How to write form to has_many association without nested_form?

I have a Patient model. 我有一个病人模型。 And I want be able as a Patient adds multiple Doctors . 而且我希望能够作为一名患者添加多个Doctor

Usually how I do it by creates with nested_form for Patient(without any of his attributes) 通常,我如何通过使用Patient的nested_form创建(没有他的任何属性)

# @patient = current_patient
= simple_nested_form_for @patient do |form|
  = form.simple_fields_for :doctors, @patient.doctors do |f|
    = f.input :first_name
    = f.input :last_name
  = form.link_to_add :add, :doctors
  = form.button :submit

And I am using PatientsController * #update * action to add and remove doctors from them. 而我使用PatientsController * #UPDATE *行动向他们添加和删除的医生。

I have already one PatientsController with edit action to edit Patient information. 我已经有一个带有编辑操作的PatientController ,可以编辑患者信息。

I think right controller to add multiple Doctors to Patient should be DoctorsController . 我认为向患者添加多个Doctor的正确控制器应该是DoctorsController But this will not work with form above which always update Patient object. 但这不适用于始终更新Patient对象的表格。

So how this form should looks like? 那么这种形式应该是什么样的呢? What is correct pattern for that? 正确的模式是什么?

Gem: https://github.com/ryanb/nested_form 宝石: https//github.com/ryanb/nested_form

Checkout Ryan's railscast #196 Nested Model Form (revised) where he shows what you are trying to do. 签出Ryan的railscast #196嵌套模型表单(修订版) ,其中他显示了您要执行的操作。 Basically, instead of Patient and Doctors he uses Question and Answers. 基本上,他使用“问题和答案”代替“患者和医生”。 Jump to minute 10:00 to see it in action and confirm that is what you are looking for. 跳至10:00分钟,以查看实际操作并确认您正在寻找的内容。 The correct controller is Patients in this example. 在此示例中,正确的控制器是患者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM