2010-08-18 2 views
1

Недавно я создал довольно глубокую вложенную форму, используя 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 

Казалось, все прекрасно работает на рельсах 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; 
    }); 
}); 

Я включил GIST с некоторыми более соответствующих страниц, если это поможет: http://gist.github.com/536704

+0

Пожалуйста, измените свой вопрос и форматировать код, чтобы сделать его доступным для чтения. –

ответ

5

вбежал в такой же выпуск. В рельсах 3 вы должны использовать символы. Так что используйте:

content_for :"#{association}_fields_template" do 

и

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

(удалить бесполезные content_given? метод, который является теперь встроенный в content_for?).

использовать также

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

вместо

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

спасибо! разрешил мою проблему, поскольку я работал в одном и том же. благодаря!! –