Permalink
Browse files

Agrego archivos para manejo de formularios anidados

  • Loading branch information...
1 parent d342b85 commit fca03581cd17dabae3f4f770704fdefa950e7ca7 @movihus committed Feb 15, 2012
Showing with 34 additions and 0 deletions.
  1. +17 −0 app/assets/javascripts/formularios_anidados.js
  2. +17 −0 app/helpers/formularios_anidados_helper.rb
@@ -0,0 +1,17 @@
+/*
+* Utilizados para agregar y sacar nuevos elementos
+ * en los formularios anidados
+ *
+ * nueva versión de agregar/quitar anidados
+ */
+function remove_fields(link) {
+ $(link).prev("input[type=hidden]").val("1");
+ $(link).parent().hide();
+}
+
+function add_fields(link, association, content) {
+ var new_id = new Date().getTime();
+ var regexp = new RegExp("new_" + association, "g");
+ $(link).before(content.replace(regexp, new_id));
+}
+
@@ -0,0 +1,17 @@
+module FormulariosAnidadosHelper
+
+ def link_to_remove_fields(name, f)
+ f.hidden_field(:_destroy) + link_to(name, "#", :class => "icon icon-del borrar_doc")
+ end
+
+ def link_to_add_fields(name, f, association, options = {})
+ options[:partial] ||= "campos_" + association.to_s.singularize
+ options[:locals] = options[:locals].is_a?(Hash) ? options[:locals].merge({ :dinamico => true }) : { :dinamico => true }
+
+ new_object = f.object.class.reflect_on_association(association).klass.new
+ fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
+ render(:partial => options[:partial], :locals => options[:locals].merge({ :f => builder }))
+ end
+ link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"), :class => "icon icon-add", :id => "add_#{association}")
+ end
+end

0 comments on commit fca0358

Please sign in to comment.