2010-08-18 8 views
1

J'ai récemment créé un formulaire imbriqué assez profond en utilisant http: // github.com/timriley/complex-form-examples pour vous guider. La forme utilise partielle ce qui suit afin de rendre les nouveaux champs quand un lien est cliqué:Rails 3 content_for issues

<%= yield :warehouses_fields_template %> 
<%= yield :ratgrades_fields_template %> 

Le contenu de ceux-ci est générée dans le ApplicationHelper en tant que tel:

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 

Tout semblait fonctionner parfaitement sur les rails 2.3.8, mais après la mise à niveau vers Rails 3 rc aujourd'hui, les modèles ne sont plus chargés. Est-ce que quelque chose a changé qui rendrait le code ci-dessus invalide? Quelqu'un d'autre remarque le même problème?

Toute aide serait grandement appréciée, Merci!

Pour référence, c'est le code jQuery concerné ainsi:

$(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; 
    }); 
}); 

J'ai inclus un Gist avec un peu plus des pages pertinentes si ça vous aidera: http://gist.github.com/536704

+0

S'il vous plaît modifier votre question et formater votre code pour le rendre lisible. –

Répondre

5

a couru dans la même problème. Dans les rails 3, vous devez maintenant utiliser des symboles. Il faut donc utiliser:

content_for :"#{association}_fields_template" do 

et

end unless content_for?(:"#{association}_fields_template") 

(supprimer la méthode content_given? inutile qui est maintenant le content_for? intégré).

utilisent également

<%= content_for :warehouses_fields_template %> 
<%= content_for :ratgrades_fields_template %> 

au lieu de

<%= yield :warehouses_fields_template %> 
<%= yield :ratgrades_fields_template %> 
+0

merci! résolu mon problème car je courais dans la même chose. Merci!! –