Question

I have a form that's part of a dynamic table. The view is typically generated from the controller like below:

  = form_for manual_coa_account, url: manual_coa_account_path(manual_coa_account), :html => {:method => 'PUT'} do |f|
    %td{:style => 'width:40px'}
      = f.text_field(:amount, :size => 11, :value => to_currency(f.object.amount))
    %td{:style => 'width:20px'}
      .control-group
        %button.btn.btn-default{:type => "submit"} Update

The issue I'm having is that the Update button does not seem to do anything when I generate a new button dynamically and append it to the table. My simplified version (removed all the irrelevant and tag for readability) is below:

  tableRowHtml = "<form accept-charset='UTF-8' action='/manual_coa_accounts/" + data.id + "' class='edit_manual_coa_account' id='edit_manual_coa_account_" + data.id + "' method='post'></form>" +"<td>"+"<input id='manual_coa_account_amount' name='manual_coa_account[amount]' size='11' type='text' value="+ data.amount + ">"+"</td>"+"<td><div class='control-group'><button class='btn btn-default' type='submit'>Update</button></div>"

  $(tableRowHtml).appendTo("#table tbody");

How do I generate a dynamic form that can submit? Is there some rails callback that I'm missing?

Was it helpful?

Solution

You can try to use event delegation here:

$('#table').on('click', 'input[type="submit"]', function() {
     // Your code to submit form here
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top