0

我有以下代码在我的发票模型中创建嵌套模型 (invoice_line_items):

    = nested_form_for @invoice, mutipart: true, class: "form-horizontal" do |f|

    ...

    %table.table.table-bordered{:id => "line-items"}
      %thead
        %tr
          %th
          %th Description
          %th Quantity
          %th Rate
      %tbody
        %tr
          = f.fields_for :invoice_line_items do |line_item|
            %td= line_item.link_to_remove "X"
            %td= line_item.text_field :description
            %td= line_item.text_field :quantity, :class => "input-mini"
            %td= line_item.text_field :rate, :class => "input-mini"
    = f.link_to_add "Add", :invoice_line_items, :data => { :target => "#line-items" }

我有两个问题:1)当我通过单击“添加”添加新行时......它与表格格式不匹配并且不插入表格中。我尽一切努力让它工作,但它没有。我还尝试添加 ryanb 在他的 gem 文档中提到的“:target”。2) 我想在发票页面上为用户准备 3 个 invoice_line_items,但我不知道该怎么做。

编辑:自从我一直在玩它以来,我现在有了一些不同的东西。我不认为我做对了,但现在每次我点击“添加”时它都会创建一个新表单:

    .row-fluid
  = f.fields_for :invoice_line_items, :wrapper => false do |line_item|
    %table.table.table-bordered#tasks
      %thead
        %th
        %th Description
        %th Quantity
        %th Rate
      %tr.fields
        %td= line_item.link_to_remove "X"
        %td= line_item.text_field :description
        %td= line_item.text_field :quantity, :class => "input-mini"
        %td= line_item.text_field :rate, :class => "input-mini"
  .row-fluid
    = f.link_to_add "Add", :invoice_line_items, :data => { :target => "#tasks" }
4

1 回答 1

1

这是一个很难弄清楚的问题,但事实证明,nested_form gem 的最新版本没有附带最新版本的 jquery_nested_form.js 文件(即使文档说要使用这个函数)。所以,要让它工作,我必须将它添加到我的 application.js 中:

$(function ($) {
  window.NestedFormEvents.prototype.insertFields = function(content, assoc, link) {
    var target = $(link).data('target');
    if (target) {
      return $(content).appendTo($(target));
    } else {
      return $(content).insertBefore(link);
    }
  };
});

这覆盖了 jquery_nested_form.js 中的函数,现在一切正常。

于 2013-04-03T18:49:25.363 回答