Rails has_many:通过嵌套表单

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)

  • 不是也必须强参数允许医生的姓名属性吗? (2认同)

cti*_*y79 6

"我得到了它的工作.我只是按照以下方式更改了模型":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)


And*_*irk 4

您的患者类别接受医生和预约的嵌套属性。尝试添加另一种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)

  • 我成功了。我刚刚更改了模型,如下所示: `class Patient &lt; ActiveRecord::Base has_many :appointments, :dependent =&gt; :destroy has_many :Physicians, :through =&gt; :appointments Accepts_nested_attributes_for :appointments end class Appointment &lt; ActiveRecord::BaseBelongs_to :PhysicianBelongs_to :患者接受嵌套属性:医生结束` (3认同)
  • 我正在处理类似的情况,但我不断遇到错误。您可以发布最终的工作代码吗?也许作为答案?我怀疑这不仅对我有帮助,而且对处理同样问题的其他人也有帮助。 (3认同)