Ruby on rails Rails有很多:通过嵌套形式

Ruby on rails Rails有很多:通过嵌套形式,ruby-on-rails,nested-forms,has-many-through,Ruby On Rails,Nested Forms,Has Many Through,我刚刚通过协会跳进了《代码》杂志。我正试图实现通过一个表单保存所有3个表(medicine、Patient和association table)的数据的功能 我的迁移: 我的视图(new.html.rb): 我可以为预约创建新的患者,医生和相关记录,但现在我希望表单中也有预约日期的字段。我应该将约会的字段放在哪里?我的控制器需要做哪些更改?我试着用谷歌搜索,试了又试,但在实现时遇到了一些或其他的错误 您的patient类接受医生和预约的嵌套属性。尝试为约会的方法添加另一个字段 &

我刚刚通过协会跳进了《代码》杂志。我正试图实现通过一个表单保存所有3个表(
medicine
Patient
和association table)的数据的功能

我的迁移: 我的视图(
new.html.rb
):



我可以为
预约创建新的
患者
医生
和相关记录
,但现在我希望表单中也有
预约日期的字段。我应该将
约会
的字段放在哪里?我的控制器需要做哪些更改?我试着用谷歌搜索,试了又试,但在实现时遇到了一些或其他的错误

您的patient类接受医生和预约的嵌套属性。尝试为约会的
方法添加另一个
字段

<% 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 %>



“我让它工作起来了。我只是按照如下方式更改了模型”:在上面的评论中引用了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
class Patient:销毁
有许多:医生,:通过=>:预约
接受约会的\u嵌套\u属性\u
结束
班级约会
好吧,这个小问题困扰了我几个小时,所以我打算在这里发布我的工作解决方案,希望它能为偷窥节省一些时间。这是针对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 %>
  <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 %>



另一个提示:您可以使用
t.references:patient
而不是
t.integer:patient\u id
来创建适当类型的列。请参见thanx@andrew以获得快速回复。我尝试了这个代码,结果有两条记录被插入到预约表中:在第一条记录中,病人id和预约日期被保存,在第二条记录中,医生id和病人id被保存。需要对控制器或模型进行任何更改吗?@Shruti我认为您应该删除
@patient.appointment.build
,在创建操作中,只需使用
@patient.save
保存对象即可。@kien如果这样做,我的表单上就看不到约会日期文本字段:(我让它工作起来了。我只是改变了模型如下:
class Patient:destroy有很多:内科医生,:through=>:预约接受嵌套的\u属性\u for:预约结束类预约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
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 %>
  <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 %>