javascript - Dynamically generated field in nested form added twice Rails -
i have 2 models, first grants second specs. specs has 2 fields ineligibility , eligibility , nested grants. have set fine need modifying jquery because every time click "add ineligibility", eligibility added. doesn't show on form show blank bullet on show view. when go edit form see blank field eligibility. if enter new text eligibility both fields save correct text. want able add 1 field without automatically adding other. jquery , helper code comes railscasts pro #196 nested model form. i'm new rails , jquery appreciated!
jquery -> $('form').on 'click', '.add_fields', (event) -> time = new date().gettime() regexp = new regexp($(this).data('id'), 'g') $(this).before($(this).data('fields').replace(regexp, time)) event.preventdefault()
and helper method:
module applicationhelper def link_to_add_fields(name, f, association) new_object = f.object.send(association).klass.new id = new_object.object_id fields = f.fields_for(association, new_object, child_index: id) |builder| render(name.to_s.singularize + "_fields", f: builder) end link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")}) end
i wanted share solution might trying same thing. mistake made thinking have several fields in each model save 1 of fields database. every time add field saves whole record other fields if blank. why every time add eligibility blank ineligibility field. solution break model , create new model every field needed.
since real goal have bulleted lists of eligibility , ineligibility came 2 other solutions handle formatting. first use rich text editor , other use .gsub add in formatting after fact. example:
<p> <strong>eligibility:</strong> <%= (@grant.eligibility).gsub(/[=+~]/, '=' => '<ul>','~' => '</ul>', '+' => '<li/>').html_safe %> </p>
Comments
Post a Comment