Created
November 16, 2010 05:48
-
-
Save avit/701493 to your computer and use it in GitHub Desktop.
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
function remove_fields(link) { | |
$(link).prev("input[type=hidden]").val("1"); | |
$(link).closest('.repeating_fields').slideUp(); | |
} | |
function add_fields(link, association, content) { | |
var newId = new Date().getTime(); | |
var regexp = new RegExp("new_" + association, "g"); | |
var newItem = $(content.replace(regexp, newId)).hide(); | |
$(link).prev('.fields').append(newItem); | |
newItem.slideDown().find('input:first').focus(); | |
} |
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
- form_for @mortgage_application do |f| | |
%fieldset#mortgage_fields | |
%legend Existing Mortgages | |
%ul | |
- f.fields_for :properties do |fa4| | |
%li.repeating_fields | |
%fieldset | |
%legend Mortgage Details | |
= f.label :lender | |
= f.text_field :lender | |
= f.label :mortgage_number | |
= f.text_field :mortgage_number | |
= f.label :agent_name | |
= f.text_field :agent_name | |
= link_to_remove_fields("Remove", f) | |
= link_to_add_fields "Add another mortgage", f, :properties |
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
def link_to_remove_fields(name, f) | |
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)", :class => "icon_button only_icon_button remove_button") | |
end | |
def link_to_add_fields(name, f, association, options={}) | |
options.merge! :class => "icon_button text_icon_button add_button" | |
association_class = f.object.class.reflect_on_association(association).klass | |
new_object = association_class.new | |
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| | |
render new_object.class.table_name.to_s + '/fields', :f => builder | |
end | |
link_to_function name, h("add_fields(this, '#{association}', '#{escape_javascript(fields)}')"), options | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment