ryanb / nested_form

Rails plugin to conveniently handle multiple models in a single form.
MIT License
1.79k stars 505 forks source link

Multiple add fields due to turbolinks on Rails 4 #286

Closed tohyongcheng closed 10 years ago

tohyongcheng commented 11 years ago

After navigating through a few pages, the form adds more than 1 field into the nested form. It is mostly due to turbolinks on Rails 4.

More information on how to solve it may be here: https://github.com/kossnocorp/jquery.turbolinks

I have tried it, but its not working with this gem. We may need to edit the js of the nested_form gem.

leckylao commented 11 years ago

I don't have issue using it on Rails 4. In addition the JS is binding on the document. So it should work.

lest commented 10 years ago

@tohyongcheng Looks like nested_form js is evaluated multiple times. This is possible if you put application.js in body instead of head. Turbolinks evaluates head only single time so you should consider putting javascript_include_tag into head element.

raldred commented 9 years ago

Is there anyway to improve nested form to support turbolinks properly? I always put javascript after all content to improve page load performance. https://developer.yahoo.com/performance/rules.html#js_bottom

amnesia7 commented 9 years ago

If the script is in the body and evaluated every time then changing the bottom of the nested_form.js file to be the following seems to work:

  window.nestedFormEvents = new NestedFormEvents();
  // Setup click handlers
  $(document)
    .on('click', 'form a.add_nested_fields',    nestedFormEvents.addFields)
    .on('click', 'form a.remove_nested_fields', nestedFormEvents.removeFields);
  // Tidy up click handlers
  $(document).on('page:after-remove', function () {
    $(document)
      .off('click', 'form a.add_nested_fields',    nestedFormEvents.addFields)
      .off('click', 'form a.remove_nested_fields', nestedFormEvents.removeFields);
  });
})(jQuery);