如 Railscast 197 中所述在表单上动态添加子字段不使用 :child_index 参数生成 html name 属性

发布于 2024-12-06 06:19:18 字数 1793 浏览 1 评论 0原文

我有一个应用程序,我试图添加记录,如 Railscast 编号 197 所示。我的对象更简单,因为我只有一层父/子关系:患者和事件。该代码可以很好地删除子记录(事件),但由于以下原因添加记录失败。我能够创建一个新对象,生成要在表单上显示的字段,并且表单看起来不错。然而,生成的 html 中的 name 属性缺少 :child_index 。生成的 html 示例如下:

<textarea cols="30" id="patient_events_attributes_description" name="patient[events_attributes][description]" rows="3"></textarea>

现有记录的 html 为:

<textarea cols="30" id="patient_events_attributes_1_description" name="patient[events_attributes][1][description]"     rows="3">Opgepakt met gestolen goederen</textarea>

请注意,新 html 中缺少现有记录中的 [1]。当然它不应该是1,而是new_xxx,然后用一个唯一的数字替换。但生成的 html 中缺少整个 [new_xxx]。有人知道出了什么问题吗?

我正在使用 Ruby 1.9.2 和 Rails 3.0.10。我只有没有原型的 JQuery 或 query-ujs。

我正在使用的代码如下所示,但它是 Railscast 代码的副本:

    def link_to_remove_fields(name, f)
    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
  end

  def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|      # new_#{association}
      render(association.to_s.singularize + "_fields", :f => builder)
    end
    link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
  end

function remove_fields(link) {
    $(link).prev("input[type=hidden]").val = "1";
    $(link).closest(".fields").hide();
}

function add_fields(link, association, content) {
    alert(content);
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g");
  $(link).before(content.replace(regexp, new_id));
}

我无法找到任何其他评论表明该代码不起作用,所以我一定是做了一些非常错误的事情。有什么想法吗?

I have an application where I am trying to add records as shown in the Railscast number 197. My objects are more simple, as I only have one level of Parent/child relationships: Patients and events. The code works fine for removing a child record (events), but adding a record fails for the following reason. I am able to create a new Object, generate the fields to display on the form, and the form looks ok. However the :child_index is missing from the name attribute in the generated html. An example of the html generated is:

<textarea cols="30" id="patient_events_attributes_description" name="patient[events_attributes][description]" rows="3"></textarea>

The html of an existing record is:

<textarea cols="30" id="patient_events_attributes_1_description" name="patient[events_attributes][1][description]"     rows="3">Opgepakt met gestolen goederen</textarea>

Note that the [1] in the existing record is missing in the new html. Of course it should not be 1, but new_xxx which is then replaced by a unique number. But the whole [new_xxx] is missing in the generated html. Does anyone have an idea of what is wrong?

I am using Ruby 1.9.2, with Rails 3.0.10. I have only JQuery with no prototype, or query-ujs.

The code I'm using is shown here below, but it is a copy of the Railscast code:

    def link_to_remove_fields(name, f)
    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
  end

  def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|      # new_#{association}
      render(association.to_s.singularize + "_fields", :f => builder)
    end
    link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
  end

function remove_fields(link) {
    $(link).prev("input[type=hidden]").val = "1";
    $(link).closest(".fields").hide();
}

function add_fields(link, association, content) {
    alert(content);
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g");
  $(link).before(content.replace(regexp, new_id));
}

i haven't been able to find any other comments that this code does not work, so I must be doing something very wrong. Any ideas out there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2024-12-13 06:19:18

查看fields_for的源码后,发现了两个问题:
1. 不使用:child_index参数,而是使用:index选项。
2. fields_for 仅在传递的对象是活动记录对象或数组时生成正确的 html。我更改了传递给类型数组的参数,使用新的空白对象作为 [0] 条目。

值得注意的是,没有关于此功能的文档。这使得 ROR 使用起来非常耗时,除非你出生在那里。

最终运行的代码如下。请注意,如果要添加更多记录,则必须将“99”替换为唯一的数字。

我还没有完全工作,因为 @patent.update_attributes(params[:patent]) 给出了一些错误,但最糟糕的部分(添加 html)已修复。

def link_to_add_fields(name, g, association)
new_object = []
new_object[0] = g.object.class.reflect_on_association(association).klass.new
fields = g.fields_for(association, new_object, :index => '99') do |builder|      # , {:child_index => "new_#{association}"} new_#{association}
  render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")

结尾

After looking at the source code for fields_for, I found the two problems:
1. The :child_index parameter is not used, instead of that the :index option is used.
2. fields_for only generates the correct html if the object passed is an active record object, or an array. I changed the parameter passed to type array, with the new blank object as the [0] entry.

It is remarkable that there is no documentation of this feature. It makes ROR very time consuming to use, unless you were born there.

The code that finally works is as follows. Note that the '99' must be replace with a unique number if more records are to be added.

I still haven't go it completely working as the @patient.update_attributes(params[:patient]) gives some errors, but the worst part (adding the html) is fixed.

def link_to_add_fields(name, g, association)
new_object = []
new_object[0] = g.object.class.reflect_on_association(association).klass.new
fields = g.fields_for(association, new_object, :index => '99') do |builder|      # , {:child_index => "new_#{association}"} new_#{association}
  render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")

end

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文