Rails 4: Adding child_index to dynamically added (nested) form fields with Cocoon Gem

前端 未结 2 617
隐瞒了意图╮
隐瞒了意图╮ 2021-02-15 11:07

UPDATED: I am trying to add/remove form fields to a nested form involving multiple models. I have seen the \"Dynamic Forms\" railscast by Ryan Bates and I have referred to this

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-15 11:36

    add this in your js.coffe file
    $(document).on 'click', 'form .remove_', (event) ->
    $(this).prev('input[type=hidden]').val('1')
    $(this).closest('fieldset').hide()
    event.preventDefault()
    
    $(document).on 'click', 'form .add_teacher', (event) ->
    event.preventDefault()
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
    

提交回复
热议问题