8

According https://github.com/nathanvda/cocoon#link_to_add_association you should be able to pass a function to data-association-insertion-node

I have tried this:

<%= link_to_add_association 'Add Timeslot', f, :timeslots, :data => {'association-insertion-node' => 'get_row()'} %>

And this:

<%= link_to_add_association 'Add Timeslot', f, :timeslots, :data => {'association-insertion-node' => 'get_row'} %>

And this (getting desperate):

<%= link_to_add_association 'Add Timeslot', f, :timeslots, :data => {'association-insertion-node' => 'function get_row(node){var row = "<tr></tr>";$("#table_body").append(row);return row;}'} %>

But none of them work.

Javascript:

function get_row(node){
    var row = "<tr></tr>"
    $("#table_body").append(row);
    return row
}

I am trying to add a tr to a table and then append the nested timeslot form to the tr.

DrevanTonder
  • 726
  • 2
  • 12
  • 32
  • Can you first explain what real world problem you are attempting to solve? I can see from the 3rd option, you clearly tried to eliminate the possibility of the file not being loaded. Did the code work before you put it in a function? – Mirv - Matt Mar 10 '17 at 12:24

1 Answers1

5

At the moment this is not possible what you are trying to do. When setting the data-association-insertion-method like this, it will just be a string in javascript, and not a reference to a method.

As documented in the README, you have to do the following (assuming you gave your links a class .add-timeslot):

$(document).on('turbolinks:load', function() {
  $(".add-timeslot a").
    data("association-insertion-method", 'append').
    data("association-insertion-node", function(link){
      return link.closest('.row').next('.row').find('.sub_tasks_form')
    });
});

(the example is almost verbatim copied from documention for demonstration purposes only --you will have to fill in your get_row function)

nathanvda
  • 49,707
  • 13
  • 117
  • 139