我最近使用http:// github.com/timriley/complex-form-examples構建了一個相當深的嵌套表單來獲得指導。窗體部分使用以下,以使新的領域時,點擊一個鏈接:Rails 3 content_for issues
<%= yield :warehouses_fields_template %>
<%= yield :ratgrades_fields_template %>
在ApplicationHelper生成,這些文件的內容,例如:
def new_child_fields_template(form_builder, association, options = {})
content_for "#{association}_fields_template" do
options[:object] ||= form_builder.object.class.reflect_on_association(association).klass.new
options[:partial] ||= association.to_s.singularize
options[:form_builder_local] ||= :f
content_tag(:div, :id => "#{association}_fields_template", :style => "display: none") do
form_builder.fields_for(association, options[:object], :child_index => "new_#{association}") do |f|
render(:partial => options[:partial], :locals => {options[:form_builder_local] => f})
end
end
end unless content_given?("#{association}_fields_template")
end
def content_given?(name)
content = instance_variable_get("@content_for_#{name}")
! content.nil?
end
一切似乎都與工作完美在rails 2.3.8上,但今天升級到Rails 3 rc後,模板不再加載。有什麼改變,會使上述代碼無效?其他人注意到同樣的問題?
任何幫助將不勝感激, 謝謝!
以供參考,這是相關的jQuery代碼,以及:
$(function() {
$('form a.add_child').live('click', function() {
// Setup
var assoc = $(this).attr('data-association'); // Name of child
var content = $('#' + assoc + '_fields_template').html(); // Fields template
// Make the context correct by replacing new_<parents> with the generated ID
// of each of the parent objects
var context = ($(this).parents('.fields').children('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
// context will be something like this for a brand new form:
// project[tasks_attributes][1255929127459][assignments_attributes][1255929128105]
// or for an edit form:
// project[tasks_attributes][0][assignments_attributes][1]
if(context) {
var parent_names = context.match(/[a-z]+_attributes/g) || []
var parent_ids = context.match(/[0-9]+/g)
for(i = 0; i < parent_names.length; i++) {
if(parent_ids[i]) {
content = content.replace(
new RegExp('(\\[' + parent_names[i] + '\\])\\[.+?\\]', 'g'),
'$1[' + parent_ids[i] + ']'
)
}
}
}
// Make a unique ID for the new child
var regexp = new RegExp('new_' + assoc, 'g');
var new_id = new Date().getTime();
content = content.replace(regexp, new_id)
$(this).parent().before(content);
return false;
});
});
我已經包含了一些更相關的網頁的要點,如果它會幫助:http://gist.github.com/536704
請編輯您的問題和格式化代碼,使其可讀。 – 2010-08-18 19:46:07