Ruby on Rails茧宝石-无法捕获回调事件 [英] Ruby on rails cocoon gem - unable to capture callback events

查看:70
本文介绍了Ruby on Rails茧宝石-无法捕获回调事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的rails应用程序中使用cocoon gem,由于我能够在表单中添加和删除嵌套的关联,因此它似乎运行良好.但是,当我尝试控制台记录回调的日志时(例如"cocoon:after-insert"),控制台上没有触发控制台日志.这可能是什么原因?如何在控制台中捕获回调事件?我的应用程序中也捆绑了gem remotipart,如果这可以提供有关为什么我无法在控制台上捕获回调的任何线索.她是我的密码:-

Hi I am currently using the cocoon gem for my rails app and it seems to be working fine since I was able to add and remove the nested associations in my form. However, when I try to console log the callbacks (e.g. "cocoon:after-insert"), there is no console log fired on the console. What could be the reason for this and how can i capture the callback event in my console? I have the gem remotipart bundled in my app too, if this would offer any clue as to why I have been unable to capture the callbacks on my console. Her are my codes:-

#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');
    });
});

推荐答案

您必须确保事件在周围的div上捕获.另外,您不需要turbolinks代码,只需执行类似的操作

You have to make sure the event is captured on a surrounding div. Also you do not need the turbolinks code, you can simply do something like

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

因此,简而言之:监听整个文档中的事件,如果我们捕获了cocoon:after-insert事件,则从.content form中记录一些内容.

So in short: listen to events on the complete document, if we capture a cocoon:after-insert event, from .content form we log something.

这篇关于Ruby on Rails茧宝石-无法捕获回调事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆