2017-02-07 19 views
1

嗨我目前正在使用我的Rails應用程序的繭寶石,它似乎工作正常,因爲我能夠添加和刪除我的窗體中的嵌套關​​聯。但是,當我試圖控制檯記錄回調(例如「cocoon:after-insert」)時,控制檯上沒有激發控制檯日誌。這可能是什麼原因以及如何捕獲我的控制檯中的回調事件?我的應用程序中還捆綁了gem remotipart,如果這能提供任何線索,說明爲什麼我無法捕獲控制檯上的回調。她是我的代碼: -Ruby on rails繭寶石 - 無法捕獲回調事件

#new.html.erb 
<div class="content"> 
    <%= form_for [@strategy,@goal], :html => {:class => 'ui form'}, :remote => true do |f| %> 
    <p> 
     <%= f.label :title %> 
     <%= f.text_field :name %> 
    </p> 

    <div id = 'tactical_field'> 
     <%= f.fields_for :tactics do |tactic| %> 
     <%= render 'tactic_fields', f: tactic %> 
     <% end %> 
    <div class="links"> 
     <%= link_to_add_association "Add", f, :tactics %> 
    </div> 
    </div> 
    <p> 
     <%= f.button :submit => "", class: "ui button" %> 
    </p> 
    <% end %> 
</div> 

#_tactics_fields.html.erb 
<div class="nested-fields"> 

    <div class='field'> 
    <%= f.label :tactic %> 
    <%= f.text_field :name %> 
    </div> 

    <div class="field"> 
    <%= f.label :rating %> 
    <%= f.text_field :rating %> 
    </div> 
    <%= link_to_remove_association "delete", f %> 
</div> 

# application.js 
$(document).on('turbolinks:load', function() { 
    $('#tactical_field').bind('cocoon:before-insert', function() { 
    console.log('helloworld'); 
    }); 
}); 
+0

嘗試將該'js'代碼移動到部分 – uzaif

回答

1

您必須確保事件在周圍的div上被捕獲。此外,你不需要turbolinks代碼,你可以簡單地這樣做

$(document).on('cocoon:after-insert', '.content form', function(e) { 
    console.log('Something'); 
}); 

因此,在短期:聽取完整的文檔事件,如果我們捕獲cocoon:after-insert事件,從.content form我們記錄一些東西。