小编Adi*_*kar的帖子

事件未在首页加载时加载,但在刷新后有效

我正在使用Rails 7创建一个名为 Employee Management System 的应用程序。为了添加员工,我创建了一个表单。在这里,我使用nested-form-fields gem 来添加员工的联系人。问题是当第一次加载表单时,当我想添加或删除联系人字段时,它会重定向到同一表单。但是当我刷新页面时,它就开始工作,没有任何问题。即使添加或删除的字段也会反映在数据库中。仅在首次加载创建或更新员工页面时才会出现此问题。我发现当我单击添加员工的链接时,表单将打开。事件不会以 html 形式加载。正如我们在下图中靠近 body 标签所看到的:

在此输入图像描述

但刷新后,事件被加载: 在此输入图像描述

我在 gem 文件夹中看到了 js.coffee 文件,但这对于像我这样的初学者来说太高级了。那里的代码如下所示:

window.nested_form_fields or= {}

nested_form_fields.bind_nested_forms_links = () ->
  $('body').off("click", '.add_nested_fields_link')
  $('body').on 'click', '.add_nested_fields_link', (event, additional_data) ->
    $link = $(this)
    object_class = $link.data('object-class')
    association_path = $link.data('association-path')
    added_index = $(".nested_#{association_path}").length
    $.event.trigger("fields_adding.nested_form_fields",{object_class: object_class, added_index: added_index, association_path: association_path, additional_data: additional_data});
    if $link.data('scope')
      $template = $("#{$link.data('scope')} ##{association_path}_template")
    else
      $template = $("##{association_path}_template")
    target = $link.data('insert-into')

    template_html = $template.html()

    # insert association indexes
    index_placeholder = "__#{association_path}_index__"
    template_html = template_html.replace(new …
Run Code Online (Sandbox Code Playgroud)

javascript jquery ruby-on-rails turbolinks turbo-rails

4
推荐指数
1
解决办法
1877
查看次数