【发布时间】:2016-07-08 13:35:10
【问题描述】:
我有一个 Rails 应用程序,其中有一个看起来像这样的表单:
[ Parent list item 1 ]
[ Parent list item 2 ]
[ Parent list item 3 - expanded ]
[ Child list item 1 ]
Child inline input Child submit button
------------------
[Parent input]
Parent submit button
父实体输入始终有效。它是一个远程表单,使用remote: true。当我添加一个父对象时,它会自动与其他父对象一起添加到列表中。每个父级可以有多个子级,当用户展开相应的父级列表项(如上例)时,会显示并列出它们。用户可以通过在Child inline input中输入一个值来添加更多的孩子,这个表单也是使用remote: true。
我遇到的问题是添加子元素并不总是在第一页加载时起作用。但是,如果我刷新页面,它会起作用。我很难理解为什么会这样。
当我创建父对象时,会呈现以下js.erb:
# screen_table_id is the list with parent objects.
# localized_strings/localized_string is the tr with the object
$("#screen_table_<%= @localized_string.screen.id %>").append("<%= j render partial: 'localized_strings/localized_string', locals: { screen: @localized_string.screen, localized_string: @localized_string } %>");
# I use the best in place gem to manage inline editing
jQuery('.best_in_place').best_in_place()
localized_strings/localized_string 的相关部分如下所示:
%tbody{ id: "localized_string_parent_#{localized_string.id}"}
%tr
%td.expandable-column
Clicking this reveals the child objects
/ The list of children is initially hidden
%tbody.hidden[localized_string]
- if localized_string.translations.any?
/ Renders the children
%tr
/ This form doesn't work on page load, after I have added the parent
= render "translations/inline_form", app: localized_string.screen.app, screen: localized_string.screen, localized_string: localized_string, translation: localized_string.translations.build
而translations/inline_form 看起来像这样:
= form_for [app, screen, localized_string, translation], remote: true do |f|
%td{ colspan: 2 }
.inline-form-group
= f.text_field :value, class: "form-control inline-form-control", placeholder: "Translation value", id: "localized_string_input_# {localized_string.id}"
%td
/ Sometimes nothing happens when I click Submit.
= f.submit 'Add translation', class: "btn ban-success-outline"
故障流程如下所示:
- 页面加载和我创建一个父对象 (
LocalizedString) - 它已正确添加到列表中。
- 扩展新的父列表元素按预期工作。
- 当点击孩子 (
Translation) 的提交按钮时,nothing.
希望我的问题可以理解。如果您有任何 cmets 或需要澄清,请发表评论。我对所有的想法都充满信心。
【问题讨论】:
标签: javascript jquery html ruby-on-rails ajax