Ruby on rails 多重嵌套形式

Ruby on rails 多重嵌套形式,ruby-on-rails,ruby,Ruby On Rails,Ruby,我已经实现了基于Ryan Bates屏幕投射的复杂嵌套表单 我在同一个表单中有两个嵌套表单 一个用于客户联系人,另一个用于客户预约 为此我有 患者模型 class Patient < ActiveRecord::Base has_many :contacts, :dependent => :destroy accepts_nested_attributes_for :contacts, :reject_if => lambda { |a| a[:content].bla

我已经实现了基于Ryan Bates屏幕投射的复杂嵌套表单

我在同一个表单中有两个嵌套表单

一个用于客户联系人,另一个用于客户预约

为此我有

患者模型

class Patient < ActiveRecord::Base
  has_many :contacts, :dependent => :destroy
  accepts_nested_attributes_for :contacts, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

  has_many :appointments, :dependent => :destroy
  accepts_nested_attributes_for :appointments, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
Application.js

function remove_fields(link) {  
    $(link).prev("input[type=hidden]").val("1");  
    $(link).closest(".fields").hide();  
}  

function add_fields(link, association, content) {  
    var new_id = new Date().getTime();  
    var regexp = new RegExp("new_" + association, "g");  
    $(link).parent().before(content.replace(regexp, new_id));  
}
我使用的是rails 3.0.3和ruby 1.9.2

contac有效,但预约无效。没有错误消息,只是没有运行include语句

这是我在表单中输入数据并输入submit时的输出

Started POST "/patients" for 127.0.0.1 at 2011-09-17 16:59:46 -0700
  Processing by PatientsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"AEhUmtE5vIMrjHYWvGWRzlDc2hKrN0nc9gXPCSlIz50=", "patient"=>{"first_name"=>"test", "middle"=>"", "last_name"=>"", "dob(1i)"=>"2011", "dob(2i)"=>"9", "dob(3i)"=>"17", "address1"=>"", "address2"=>"", "city"=>"", "state"=>"", "country"=>"", "insurance_name"=>"", "contacts_attributes"=>{"0"=>{"contact_type"=>"cell", "content"=>"999-999-9999", "_destroy"=>"false"}, "1"=>{"contact_type"=>"", "content"=>"", "_destroy"=>"false"}}, "appointments_attributes"=>{"0"=>{"appointment_date(1i)"=>"2011", "appointment_date(2i)"=>"9", "appointment_date(3i)"=>"17", "appointment_date(4i)"=>"23", "appointment_date(5i)"=>"59", "_destroy"=>"false"}}, "notes"=>""}, "commit"=>"Create Patient"}
  SQL (0.9ms)  BEGIN
  SQL (1.2ms)  describe `patients`
  AREL (1.5ms)  INSERT INTO `patients` (`first_name`, `middle`, `last_name`, `dob`, `address1`, `address2`, `city`, `state`, `country`, `notes`, `insured`, `insurance_name`, `created_at`, `updated_at`) VALUES ('test', '', '', '2011-09-17 00:00:00', '', '', '', '', '', '', NULL, '', '2011-09-17 23:59:46', '2011-09-17 23:59:46')
  SQL (3.2ms)  describe `contacts`
  AREL (0.3ms)  INSERT INTO `contacts` (`patient_id`, `contact_type`, `content`, `created_at`, `updated_at`) VALUES (8, 'cell', '999-999-9999', '2011-09-17 23:59:46', '2011-09-17 23:59:46')
  SQL (0.4ms)  COMMIT
Redirected to http://localhost:3000/patients/8
Completed 302 Found in 74ms
如果我在控制台模式下的代码中这样做

@patients = Patient.find_by_id(1)
@patients.appointment
它返回[],表示关系正确

哦,我试着评论一下我的联系方式,但还是能让它发挥作用


任何想法???

在您的患者模型中,在预约的嵌套声明属性中,如果不存在“内容”属性,您有一个拒绝属性

content属性不是约会的参数,它只是联系人的参数。例如,当您发送属性,并且您的患者模型看到约会具有空白内容属性时,它会拒绝约会的嵌套属性

要解决此问题,请将reject_if子句从用于声明的委任嵌套_属性_中删除

accepts_nested_attributes_for :appointments, :allow_destroy => true
或对现有属性进行更改。比如任命日期


让我知道这是否是问题所在。

在您的患者模型中,在预约的嵌套声明属性中,对于不存在的“内容”属性,您有一个拒绝属性

content属性不是约会的参数,它只是联系人的参数。例如,当您发送属性,并且您的患者模型看到约会具有空白内容属性时,它会拒绝约会的嵌套属性

要解决此问题,请将reject_if子句从用于声明的委任嵌套_属性_中删除

accepts_nested_attributes_for :appointments, :allow_destroy => true
或对现有属性进行更改。比如任命日期


让我知道这是否是问题所在。

太棒了!!谢谢。。。我记不清我读了多少次这段代码,却看不到它。复制和粘贴有时确实会造成阻碍,而不是节省时间。如果我写了这篇文章,那么从复制到粘贴的时间会多出一分钟。。。相反,我花了4个小时试着调试它。没问题,很高兴我能帮上忙。。顺便说一句,寻求帮助是一个很好的开始。开始更频繁地这样做。欢迎:)太棒了!!谢谢。。。我记不清我读了多少次这段代码,却看不到它。复制和粘贴有时确实会造成阻碍,而不是节省时间。如果我写了这篇文章,那么从复制到粘贴的时间会多出一分钟。。。相反,我花了4个小时试着调试它。没问题,很高兴我能帮上忙。。顺便说一句,寻求帮助是一个很好的开始。开始更频繁地这样做。欢迎:)
module ApplicationHelper
  def link_to_remove_fields(name, f)
    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
  end

  def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => builder)
    end
    link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')")
      end

end
function remove_fields(link) {  
    $(link).prev("input[type=hidden]").val("1");  
    $(link).closest(".fields").hide();  
}  

function add_fields(link, association, content) {  
    var new_id = new Date().getTime();  
    var regexp = new RegExp("new_" + association, "g");  
    $(link).parent().before(content.replace(regexp, new_id));  
}
Started POST "/patients" for 127.0.0.1 at 2011-09-17 16:59:46 -0700
  Processing by PatientsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"AEhUmtE5vIMrjHYWvGWRzlDc2hKrN0nc9gXPCSlIz50=", "patient"=>{"first_name"=>"test", "middle"=>"", "last_name"=>"", "dob(1i)"=>"2011", "dob(2i)"=>"9", "dob(3i)"=>"17", "address1"=>"", "address2"=>"", "city"=>"", "state"=>"", "country"=>"", "insurance_name"=>"", "contacts_attributes"=>{"0"=>{"contact_type"=>"cell", "content"=>"999-999-9999", "_destroy"=>"false"}, "1"=>{"contact_type"=>"", "content"=>"", "_destroy"=>"false"}}, "appointments_attributes"=>{"0"=>{"appointment_date(1i)"=>"2011", "appointment_date(2i)"=>"9", "appointment_date(3i)"=>"17", "appointment_date(4i)"=>"23", "appointment_date(5i)"=>"59", "_destroy"=>"false"}}, "notes"=>""}, "commit"=>"Create Patient"}
  SQL (0.9ms)  BEGIN
  SQL (1.2ms)  describe `patients`
  AREL (1.5ms)  INSERT INTO `patients` (`first_name`, `middle`, `last_name`, `dob`, `address1`, `address2`, `city`, `state`, `country`, `notes`, `insured`, `insurance_name`, `created_at`, `updated_at`) VALUES ('test', '', '', '2011-09-17 00:00:00', '', '', '', '', '', '', NULL, '', '2011-09-17 23:59:46', '2011-09-17 23:59:46')
  SQL (3.2ms)  describe `contacts`
  AREL (0.3ms)  INSERT INTO `contacts` (`patient_id`, `contact_type`, `content`, `created_at`, `updated_at`) VALUES (8, 'cell', '999-999-9999', '2011-09-17 23:59:46', '2011-09-17 23:59:46')
  SQL (0.4ms)  COMMIT
Redirected to http://localhost:3000/patients/8
Completed 302 Found in 74ms
@patients = Patient.find_by_id(1)
@patients.appointment
accepts_nested_attributes_for :appointments, :allow_destroy => true