gpt4 book ai didi

ruby-on-rails - Rails has_many :through nested form

转载 作者:行者123 更新时间:2023-12-03 11:39:28 24 4
gpt4 key购买 nike

我刚跳进去has_many :through协会。我正在尝试通过单个表单实现为所有 3 个表( PhysicianPatient 和关联表)保存数据的能力。

我的迁移:

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 < ActiveRecord::Base
has_many :appointments
has_many :physicians, :through => :appointments
accepts_nested_attributes_for :appointments
accepts_nested_attributes_for :physicians
end
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
accepts_nested_attributes_for :patients
accepts_nested_attributes_for :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end

我的 Controller :
def new
@patient = Patient.new
@patient.physicians.build
@patient.appointments.build
end

我的观点( new.html.rb):
<% form_for(@patient) do |patient_form| %>
<%= patient_form.error_messages %>
<p>
<%= patient_form.label :name, "Patient Name" %>
<%= patient_form.text_field :name %>
</p>
<% patient_form.fields_for :physicians do |physician_form| %>
<p>
<%= physician_form.label :name, "Physician Name" %>
<%= physician_form.text_field :name %>
</p>
<% end %>

<p>
<%= patient_form.submit 'Create' %>
</p>
<% end %>

<%= link_to 'Back', patients_path %>

我可以创建一个新的 Patient , PhysicianAppointment 的相关记录,但现在我想为 appointment_date 设置字段形式上也一样。我应该在哪里放置 Appointment 的字段s 以及我的 Controller 需要进行哪些更改?我尝试谷歌搜索并尝试 this ,但在实现它时遇到了一些或其他错误。

最佳答案

好吧,这个问题的小家伙让我难住了几个小时,所以我将在这里发布我的工作解决方案,希望它可以为窥视节省一些时间。这适用于 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

Controller :
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 %>
<p>
<%= patient_form.label :name, "Patient Name" %>
<%= patient_form.text_field :name %>
</p>

<% patient_form.fields_for :appointments do |appointment_form| %>
<p>
<%= appointment_form.label :appointment_date, "Appointment Date" %>
<%= appointment_form.date_field :appointment_date %>
</p>

<% appointment_form.fields_for :physician do |physician_form| %>
<p>
<%= physician_form.label :name, "Physician Name" %>
<%= physician_form.text_field :name %>
</p>
<% end %>
<% end %>

<p>
<%= patient_form.submit 'Create' %>
</p>
<% end %>

<%= link_to 'Back', patients_path %>

关于ruby-on-rails - Rails has_many :through nested form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13506735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com