Created
January 3, 2012 12:53
-
-
Save stephancom/1554795 to your computer and use it in GitHub Desktop.
Generic Repeatable Fields
This file contains 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
$('a[data-add-nested]').live('click', function(){ | |
var regexp = new RegExp($(this).data('add-nested-replace'), "g") | |
target = $(this).data('add-nested-target') | |
new_stuff = $($(this).data('add-nested').replace(regexp, (new Date).getTime())).hide() | |
if(!target || target == 'before') { | |
new_stuff.insertBefore($(this)) | |
} else if(target == 'after') { | |
new_stuff.insertAfter($(this)) | |
} else { | |
new_stuff.appendTo(target) | |
} | |
new_stuff.slideDown() | |
return false | |
}) |
This file contains 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 GenericHelper | |
class GenericFormObject | |
def to_param | |
@to_param ||= "new_repeated_fields_#{Time.now.to_i}" | |
end | |
def method_missing(sym, *args, &block) end | |
end | |
def link_to_add_generic(label, f, nested, initial = 1, link_options = {}, &block) | |
new_object = GenericFormObject.new | |
reuse_fields = f.semantic_fields_for(nested, new_object) do |n| capture_haml(n, &block) end | |
returning [] do |a| | |
initial.times { |i| a << reuse_fields.gsub(new_object.to_param, i.to_s) } | |
a << link_to(label, '', link_options.merge('data-add-nested' => reuse_fields, 'data-add-nested-replace' => new_object.to_param)) | |
end.join | |
end | |
end |
This file contains 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
- semantic_form_for 'report', :url => generate_report_path, :html => {:method => :get} do |f| | |
= link_to_add_generic 'Add Field Set', f, 'field_set[]' do |g| | |
- g.inputs 'Field Set' do | |
= g.input :name | |
= g.input :pick_one, :collection => %w(red green blue) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
make repeatable fields in a form without a model using formtastic and haml