Shr*_*uti 22 ruby-on-rails nested-forms has-many-through
我刚刚加入has_many :through联盟.我试图实现保存所有3个表数据(的能力Physician,Patient通过一个单一的形式和关联表).
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
def new
@patient = Patient.new
@patient.physicians.build
@patient.appointments.build
end
Run Code Online (Sandbox Code Playgroud)
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 %>
Run Code Online (Sandbox Code Playgroud)
我能够创建一个新的Patient,Physician相关的记录Appointment,但现在我想appointment_date在表格中也有字段.我应该在哪里放置字段Appointment以及我的控制器需要进行哪些更改?我尝试谷歌搜索并尝试了这一点,但陷入了实施它的一些或其他错误.
guy*_*214 22
好吧,这个问题的小问题困扰了我几个小时,所以我打算在这里发布我的工作解决方案,希望它能刮掉一些时间偷看.这适用于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
Run Code Online (Sandbox Code Playgroud)
控制器:
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
Run Code Online (Sandbox Code Playgroud)
视图
<% 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 %>
Run Code Online (Sandbox Code Playgroud)
"我得到了它的工作.我只是按照以下方式更改了模型":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
Run Code Online (Sandbox Code Playgroud)
您的患者类别接受医生和预约的嵌套属性。尝试添加另一种fields_for预约方式。
<% 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 %>
<% patient_form.fields_for :appointments do |appointment_form| %>
<p>
<%= appointment_form.label :appointment_date, "Appointment Date" %>
<%= appointment_form.date_field :appointment_date %>
</p>
<% end %>
<p>
<%= patient_form.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', patients_path %>
Run Code Online (Sandbox Code Playgroud)