Last active
December 12, 2015 02:49
-
-
Save alvinsj/4702019 to your computer and use it in GitHub Desktop.
link_to_add_fields "Add Field", f, :fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).on 'click', 'form .remove_fields', (event) -> | |
$(this).prev('input[type=hidden]').val('1') | |
$(this).closest('fieldset').hide() | |
event.preventDefault() | |
$(document).on 'click', 'form .add_fields', (event) -> | |
time = new Date().getTime() | |
regexp = new RegExp($(this).data('id'), 'g') | |
$(this).before($(this).data('fields').replace(regexp, time)) | |
event.preventDefault() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
def link_to_add_fields(name, f, association) | |
new_object = f.object.send(association).klass.new | |
id = new_object.object_id | |
fields = f.fields_for(association, new_object, child_index: id) do |builder| | |
render(association.to_s.singularize + "_fields", f: builder) | |
end | |
link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment