Rails has_many:通过嵌套表单

我刚刚跳进了has_many :through联想。 我正在尝试通过单一表单实现为所有3个表( PhysicianPatient和关联表)保存数据的function。

我的迁移:

 class CreatePhysicians < ActiveRecord::Migration def self.up create_table :physicians do |t| t.string :name t.timestamps end end end class CreatePatients < ActiveRecord::Migration def self.up create_table :patients do |t| t.string :name t.timestamps end end end class CreateAppointments < ActiveRecord::Migration def self.up create_table :appointments do |t| t.integer :physician_id t.integer :patient_id t.date :appointment_date t.timestamps end end end 

我的模特:

 class Patient  :appointments accepts_nested_attributes_for :appointments accepts_nested_attributes_for :physicians end class Physician  :appointments accepts_nested_attributes_for :patients accepts_nested_attributes_for :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end 

我的控制器:

 def new @patient = Patient.new @patient.physicians.build @patient.appointments.build end 

我的观点( new.html.rb ):

   

我能够为Appointment创建一个新的PatientPhysician和相关记录,但是现在我想在表格中也有@_ate的字段。 我应该在哪里放置Appointment字段以及我的控制器需要进行哪些更改? 我尝试使用谷歌搜索并尝试了这一点 ,但陷入了实施它的一些或其他错误。

好吧,这个问题的小问题困扰了我几个小时,所以我打算在这里发布我的工作解决方案,希望它能刮掉一些时间来窥视。 这适用于Rails 4.0和Ruby 2.0。 这也克服了我的“符号到整数转换”问题。

楷模:

 class Patient < ActiveRecord::Base has_many :appointments has_many :physicians, :through: :appointments accepts_nested_attributes_for :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient accepts_nested_attributes_for :physician end class Physicians < ActiveRecord::Base has_many :appointments has_many :patients, through: :appointments end 

控制器:

 def new @patient= Patient.new @appointments = @patient.appointments.build @physician = @appointments.build_physician end def create Patient.new(patient_params) end def patient_params params.require(:patient).permit(:id, appointments_attributes: [:id, :appointment_time, physician_attributes: [:id ] ) end 

视图

 <% form_for(@patient) do |patient_form| %> <%= patient_form.error_messages %> 

<%= patient_form.label :name, "Patient Name" %> <%= patient_form.text_field :name %>

<% patient_form.fields_for :appointments do |appointment_form| %>

<%= appointment_form.label :appointment_date, "Appointment Date" %> <%= appointment_form.date_field :appointment_date %>

<% appointment_form.fields_for :physician do |physician_form| %>

<%= physician_form.label :name, "Physician Name" %> <%= physician_form.text_field :name %>

<% end %> <% end %>

<%= patient_form.submit 'Create' %>

<% end %> <%= link_to 'Back', patients_path %>

“我得到了它的工作。我只是改变了模型如下”:在上面的评论中引用了Shruti

 class Patient < ActiveRecord::Base has_many :appointments, :dependent => :destroy has_many :physicians, :through => :appointments accepts_nested_attributes_for :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient accepts_nested_attributes_for :physician end 

您的患者类接受医生和约会的嵌套属性。 尝试添加另一个fields_for方法进行约会。

 <% form_for(@patient) do |patient_form| %> <%= patient_form.error_messages %> 

<%= patient_form.label :name, "Patient Name" %> <%= patient_form.text_field :name %>

<% patient_form.fields_for :physicians do |physician_form| %>

<%= physician_form.label :name, "Physician Name" %> <%= physician_form.text_field :name %>

<% end %> <% patient_form.fields_for :appointments do |appointment_form| %>

<%= appointment_form.label :appointment_date, "Appointment Date" %> <%= appointment_form.date_field :appointment_date %>

<% end %>

<%= patient_form.submit 'Create' %>

<% end %> <%= link_to 'Back', patients_path %>