RailsCasts Pro episodes are now free!
Learn more or hide this
GitHub User: Seralto
Site: http://www.sergiotoledo.com.br
Just a correction in order to rewrite the ID as well:
var $new_obj = $('<fieldset></fieldset>').html($content_item.find('fieldset:first').html() .replace(new RegExp(name + '_0', "g"), name + '_' + number).replace(/\[0\]/g, '\[' + number + '\]'));
I use a different approach:
// application.js $('form').on('click', '.add-fields', function(e) { var $content_item = $(this).closest('.content-item'); var number = $content_item.find('fieldset').length; var name = $(this).data('name'); var $new_obj = $('<fieldset></fieldset>').html($content_item.find('fieldset:first').html().replace(/\[0\]/g, '\[' + number + '\]')); $(this).before($new_obj); $new_obj.find(':input').not(':hidden, :checkbox, :radio').val(''); $new_obj.find(':checkbox, :radio').prop('checked', false); e.preventDefault(); }); # _form.html.erb <%= link_to 'Add', '#', class: "add-fields", data: {name: :person_cars_attributes} %>
This way I don't need any helper_method.
Just a correction in order to rewrite the ID as well:
I use a different approach:
This way I don't need any helper_method.