我正在使用Rails 7創建一個名為 Employee Management System 的應用程式。要添加員工,我創建了一個表單。在這里,我使用了nested-form-fields gem 來添加員工的聯系人。問題是第一次加載表單時,當我想添加或洗掉聯系人欄位時,它會重定向到同一個表單。但是當我重繪 頁面時,它開始作業沒有任何問題。甚至添加或洗掉的欄位也會反映在資料庫中。僅在首次加載創建或更新員工頁面時才會產生此問題。我發現當我單擊鏈接添加員工并打開表單時。事件未在 html 中加載。正如我們在下圖中身體標簽附近的圖片中看到的那樣:

但重繪 后,會加載事件:

我在 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 RegExp(index_placeholder,"g"), added_index)
# look for replacements in user defined code and substitute with the index
template_html = template_html.replace(new RegExp("__nested_field_for_replace_with_index__","g"), added_index)
# replace child template div tags with script tags to avoid form submission of templates
$parsed_template = $(template_html)
$child_templates = $parsed_template.closestChild('.form_template')
$child_templates.each () ->
$child = $(this)
$child.replaceWith($("<script id='#{$child.attr('id')}' type='text/html' />").html($child.html()))
if target?
$('#' target).append($parsed_template)
else
$template.before( $parsed_template )
$parsed_template.trigger("fields_added.nested_form_fields", {object_class: object_class, added_index: added_index, association_path: association_path, event: event, additional_data: additional_data});
false
$('body').off("click", '.remove_nested_fields_link')
$('body').on 'click', '.remove_nested_fields_link', ->
$link = $(this)
return false unless $.rails == undefined || $.rails.allowAction($link)
return false if $link.attr('disabled')
object_class = $link.data('object-class')
delete_association_field_name = $link.data('delete-association-field-name')
removed_index = parseInt(delete_association_field_name.match('(\\d \\]\\[_destroy])')[0].match('\\d ')[0])
$.event.trigger("fields_removing.nested_form_fields",{object_class: object_class, delete_association_field_name: delete_association_field_name, removed_index: removed_index });
$nested_fields_container = $link.parents(".nested_fields").first()
delete_field = $nested_fields_container.find("input[type='hidden'][name='#{delete_association_field_name}']")
if delete_field.length > 0
delete_field.val('1')
else
$nested_fields_container.before "<input type='hidden' name='#{delete_association_field_name}' value='1' />"
$nested_fields_container.hide()
$nested_fields_container.find('input[required]:hidden, select[required]:hidden, textarea[required]:hidden').removeAttr('required')
$nested_fields_container.trigger("fields_removed.nested_form_fields",{object_class: object_class, delete_association_field_name: delete_association_field_name, removed_index: removed_index});
false
$(document).on "page:change turbolinks:load", ->
nested_form_fields.bind_nested_forms_links()
jQuery ->
nested_form_fields.bind_nested_forms_links()
#
# * jquery.closestchild 0.1.1
# *
# * Author: Andrey Mikhaylov aka lolmaus
# * Email: lolmaus@gmail.com
# *
#
$.fn.closestChild = (selector) ->
$children = undefined
$results = undefined
$children = @children()
return $() if $children.length is 0
$results = $children.filter(selector)
if $results.length > 0
$results
else
$children.closestChild selector
有什么可以改變來解決這個問題嗎?請幫忙!!
uj5u.com熱心網友回復:
在我的應用程式上作業了 2 天后,當我將 jquery 添加到 application.js 以添加一個使永久地址和本地地址相同的復選框時,同樣的問題出現了。所以我得出的結論是,這個問題不是因為 gem,而是其他一些原因。所以我google了一下,發現turbolink是罪魁禍首。在 turbo-rails github 存盤庫中搜索后,在“已關閉”類別中發現了該問題。通過在 application.html.erb 中的 head 部分添加一個簡單的行,閱讀了該問題。線路是:
<meta name="turbo-visit-control" content="reload">
雖然,有問題。添加此行后,由于重新加載,閃存訊息消失了。所以我再次谷歌搜索以找出更好的方法。洗掉了這一行,并在 application.js 中進行了更改
import "@hotwired/turbo-rails"
到
import { Turbo } from "@hotwired/turbo-rails" Turbo.session.drive = false.
這是我在 turbo-rails readme.md 中找到的。并且無需重繪 頁面即可重新出現 Flash 訊息。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/426359.html
