Created
June 26, 2018 05:52
-
-
Save vtno/60739dff68e5b442bbfc94b0d403898d to your computer and use it in GitHub Desktop.
Generate button with html template for dynamic form addition.
This file contains hidden or 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
# frozen_string_literal: true | |
module DynamicFormHelper | |
def link_to_add_association(label, options = {}, partial:, form:, association:, association_type:) | |
format = options.fetch(:format, '') | |
new_object = form.object.send(association).klass.new | |
template_opts = { | |
locals: { | |
form: form, | |
format: format, | |
label: label, | |
association: association, | |
new_object: new_object, | |
id: new_object.object_id | |
}, | |
data: { id: new_object.object_id } | |
} | |
render_opts = { | |
class: 'btn btn-sm btn-primary', | |
'data-template-type': association.to_s, | |
'data-template-for-insertion': | |
CGI.escapeHTML( | |
render(partial: partial, **template_opts) | |
), | |
'data-id': new_object.object_id, | |
'data-association-type': association_type, | |
'data-content-type': association.to_s | |
} | |
link_to label, '#', render_opts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment