diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index fcbd4074..c83b2894 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -2,6 +2,8 @@ # Helpers module ApplicationHelper + BRACKETS = /[\[\]]/ + # Devuelve el atributo name de un campo anidado en el formato que # esperan los helpers *_field # @@ -19,6 +21,14 @@ module ApplicationHelper [root, name] end + # Obtiene un ID + # + # @param base [String] + # @param attribute [String, Symbol] + def id_for(base, attribute) + "#{base.gsub(BRACKETS, '_')}_#{attribute}".squeeze('_') + end + def plain_field_name_for(*names) root, name = field_name_for(*names) diff --git a/app/views/posts/attributes/_new_array.haml b/app/views/posts/attributes/_new_array.haml index 08fc6dee..b4407c0d 100644 --- a/app/views/posts/attributes/_new_array.haml +++ b/app/views/posts/attributes/_new_array.haml @@ -1,7 +1,7 @@ -# Genera un listado de checkboxes entre los que se puede elegir para guardar :ruby - id = "#{base.gsub(/[\[\]]/, '_')}_#{attribute}".squeeze('_') + id = id_for(base, attribute) name = "#{base}[#{attribute}][]" form_id = "form-#{Nanoid.generate}" diff --git a/app/views/posts/attributes/_new_has_many.haml b/app/views/posts/attributes/_new_has_many.haml index 48230759..9382e2af 100644 --- a/app/views/posts/attributes/_new_has_many.haml +++ b/app/views/posts/attributes/_new_has_many.haml @@ -8,7 +8,7 @@ del formulario principal porque no se pueden anidar. :ruby - id = "#{base.gsub(/[\[\]]/, '_')}_#{attribute}".squeeze('_') + id = id_for(base, attribute) name = "#{base}[#{attribute}][]" form_id = "form-#{Nanoid.generate}" modal_id = "modal-#{Nanoid.generate}" diff --git a/app/views/posts/attributes/_new_predefined_array.haml b/app/views/posts/attributes/_new_predefined_array.haml index 218a2cc2..36f1ae2f 100644 --- a/app/views/posts/attributes/_new_predefined_array.haml +++ b/app/views/posts/attributes/_new_predefined_array.haml @@ -3,8 +3,7 @@ guardar, pero no se pueden agregar nuevos. :ruby - # @todo Convertir en un helper - id = "#{base.gsub(/[\[\]]/, '_')}_#{attribute}".squeeze('_') + id = id_for(base, attribute) name = "#{base}[#{attribute}][]" form_id = "form-#{Nanoid.generate}"