Skip to content

Instantly share code, notes, and snippets.

@stephancom
Created January 3, 2012 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephancom/1554795 to your computer and use it in GitHub Desktop.
Save stephancom/1554795 to your computer and use it in GitHub Desktop.
Generic Repeatable Fields
$('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
})
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
- 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)
@stephancom
Copy link
Author

make repeatable fields in a form without a model using formtastic and haml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment