Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails cocoon添加记录后如何执行功能?_Ruby On Rails_Ruby On Rails 4_Cocoon Gem_Cocoonjs - Fatal编程技术网

Ruby on rails cocoon添加记录后如何执行功能?

Ruby on rails cocoon添加记录后如何执行功能?,ruby-on-rails,ruby-on-rails-4,cocoon-gem,cocoonjs,Ruby On Rails,Ruby On Rails 4,Cocoon Gem,Cocoonjs,我在rails项目中使用cocoon gem,我有两个“link_to_add_association”,但我需要根据按下哪个link_to来执行某个函数,link_to与id“link_1”将记录添加到表的开头,另一个将记录与其他值一起添加到表的末尾,这是我的部分形式: = form_for([@billing, @payment]) do |f| - if @payment.errors.any? = render partial: 'layouts/partials/error

我在rails项目中使用cocoon gem,我有两个“link_to_add_association”,但我需要根据按下哪个link_to来执行某个函数,link_to与id“link_1”将记录添加到表的开头,另一个将记录与其他值一起添加到表的末尾,这是我的部分形式:

= form_for([@billing, @payment]) do |f|
  - if @payment.errors.any?
    = render partial: 'layouts/partials/errors/form_errors', locals: { errors: @payment.errors }
  = f.hidden_field :billing_id, value: @billing.id
  .row
    .col-md-6
      .form-group.row
        .col-md-4
          = f.label :payment, class: 'form-control-label'
        .col-md-8
          = f.text_field :payment, class: 'form-control text-right', 'data-currency-format--live': '', 'data-target': '#total-pay'
    .col-md-6
      .form-group.row
        .col-md-4
          = f.label :day, class: 'form-control-label'
        .col-md-6
          = f.text_field :day, class: 'form-control datepicker', 'data-startView': 'days', 'data-minyear': @billing.date_bill.year, 'data-minmonth': @billing.date_bill.month, 'data-minday': @billing.date_bill.day

        .col-md-1
          .links
            = link_to_add_association 'add', f, :payment_details, partial: 'payment_detail_fields',:"data-association-insertion-node" => "tbody.details",:"data-association-insertion-method" => "prepend", class:"btn btn-success btn-xs", id: "link_1"





        .col-md-1
          .links
            = link_to_add_association 'add', f, :payment_details, partial: 'payment_detail_discount_fields',:"data-association-insertion-node" => "tbody.details",:"data-association-insertion-method" => "append", class:"btn btn-success btn-xs", id: "link_2"




    .col-md-12
      #payment_details
        %table.tabla_personalizada
          %thead
            %tr
              %th value_paid
              %th voucher
              %th accion
              %th value_paid
              %th value_paid
              %th voucher
              %th accion

          %tbody.details
            = f.fields_for :payment_details do |payment_detail|
              = render 'payment_detail_fields', f: payment_detail

            %tr
              %td 
              %td 
              %td 
              %td 
              %td 
              %td 
              %td 
            %tr.descuentos
              %td Descuento
              %td Descuento              
              %td 
              %td 
              %td 
              %td 
              %td 

  .row
    .col-md-12
      = f.label :comments, class: 'form-control-label'
      = f.text_area :comments, class: 'form-control', rows: 6
  %hr
  .row
    .col-md-8
      .form-group.row
        .col-md-8
          = f.submit :class => 'btn btn-primary' 



- content_for :javascript do
  :javascript



    $(document).ready(function() {

      $("#link_1").on("cocoon:after-insert", function() {
        alert("element of link_1");
      } ) 

      $("#link_2").on("cocoon:after-insert", function() {
        alert("element of link_2");
      } ) 

    });
我试过这个代码,但我找不到一个方法使它工作

$(document).ready(function() {

  $("#link_1").on("cocoon:after-insert", function() {
    alert("element of link_1");
  } ) 

  $("#link_2").on("cocoon:after-insert", function() {
    alert("element of link_1");
  } ) 

});

是否使用嵌套属性?@DeezNuuts是的,您必须捕获周围div上的事件。因此,不要在链接本身上定义
#link1
,而是在周围的ddiv上使用类
.links
,这是否改善了情况?