多个嵌套表单

时间:2011-09-18 00:18:03

标签: ruby-on-rails ruby

我已经实现了基于Ryan Bates Screen cast的复杂嵌套表单。

我想要以相同的形式拥有2个嵌套表格

一个是Cutomer的联系人,另一个是客户的约会

因为我有

患者模型

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

联系模式

class Contact < ActiveRecord::Base
  belongs_to :patient
end

约会模式

class Appointment < ActiveRecord::Base
  belongs_to :patient
end

appointment_fields部分表格

<div class="fields">
      Appointment: <%= f.datetime_select :appointment_date %>
        <%= link_to_remove_fields "remove", f%><br />
</div>

contact_fields部分表格

<div class="fields">
      Contact: <%= f.text_field :contact_type %>
      <%= f.text_field :content %>
        <%= link_to_remove_fields "remove", f%><br />
</div>

患者_形成部分

Edited to show only the formfields

    <%= f.fields_for :contacts do |builder| %>
        <%= render "contact_fields", :f => builder %>
    <% end %>

    <p><%= link_to_add_fields "Add More Contact", f, :contacts %></p>

    <%= f.fields_for :appointments do |builder| %>
        <%= render "appointment_fields", :f => builder %>
    <% end %>

    <p><%= link_to_add_fields "Add Appointment", f, :appointments %></p>

Application_helper

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

的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

它返回[],表明关系是正确的

哦,我试图评论联系,但仍然能够让它工作

任何想法??

1 个答案:

答案 0 :(得分:1)

在患者模型中,在约会的nested_attributes_for声明中,对于不存在的“内容”属性,您有一个reject_if。

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

要修复它,请从appointement nested_attributes_for声明中删除reject_if子句

accepts_nested_attributes_for :appointments, :allow_destroy => true

或者为现有属性更改它。比如appointement_date。

如果这是问题,请告诉我。