我已经实现了基于Ryan Bates Screen Cast的复杂嵌套形式。多重嵌套形式
我修整有相同的形式
一个是CUTOMER的联系人和其他客户的约会
为2种嵌套形式我有
患者模型
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
contact model
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>
患者_form局部
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));
}
我使用的铁轨3.0.3和1.9.2的红宝石
的联系的工作,但任命没有。任何错误消息只是不运行包含语句
这里输出的时候我在表单中输入的数据,然后输入提交
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
返回[]这说明关系是正确的
哦,我想注释掉接触,但仍然能够得到它的工作
任何想法??
真棒!非常感谢......我失去了多少次阅读这段代码的计数,并且看不清楚。复制和粘贴有时会妨碍您的工作,而不是节省时间。如果我已经写过,复制和粘贴会花费额外的时间......但我花了4个小时试图调试它 – Marrento
没问题,很高兴我可以帮助..顺便说一句,寻求帮助是一个非常好的开始。开始更频繁地做。欢迎:) – e3matheus