Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use html5 template tag instead of script/div hack #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions lib/assets/javascripts/nested_form_fields.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ nested_form_fields.bind_nested_forms_links = () ->
# look for replacements in user defined code and substitute with the index
template_html = template_html.replace(new RegExp("__nested_field_for_replace_with_index__","g"), added_index)

# replace child template div tags with script tags to avoid form submission of templates
$parsed_template = $(template_html)
$child_templates = $parsed_template.closestChild('.form_template')
$child_templates.each () ->
$child = $(this)
$child.replaceWith($("<script id='#{$child.attr('id')}' type='text/html' />").html($child.html()))

if target?
$('#' + target).append($parsed_template)
Expand Down
16 changes: 2 additions & 14 deletions lib/nested_form_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,12 @@ def fields_for_has_many_association_with_template(association_name, association,


def nested_model_template name, association_name, options, block
for_template = self.options[:for_template]

# Render the outermost template in a script tag to avoid it from being submited with the form
# Render all deeper nested templates as hidden divs as nesting script tags messes up the html.
# When nested fields are added with javascript by using a template that contains nested templates,
# the outermost nested templates div's are replaced by script tags to prevent those nested templates
# fields from form subission.
#
@template.content_tag( for_template ? :div : :script,
type: for_template ? nil : 'text/html',
id: template_id(association_name),
class: for_template ? 'form_template' : nil,
style: for_template ? 'display:none' : nil ) do
@template.content_tag( :template, id: template_id(association_name)) do
nested_fields_wrapper(association_name, options[:wrapper_tag], options[:legend], options[:wrapper_options]) do
association_class = (options[:class_name] || object.public_send(association_name).klass.name).to_s.classify.constantize
fields_for_nested_model("#{name}[#{index_placeholder(association_name)}]",
association_class.new,
options.merge(for_template: true), block)
options, block)
end
end
end
Expand Down