首页 > 解决方案 > Rails动态添加嵌套表单错误未显示在参数中

问题描述

仅首先出现在参数中,但其他我们动态添加的不显示在参数中

应用js

(function () {
    var ready;

    ready = function () {
        $(document).on('click', '.remove_fields', function (event) {
            $(this).prev('input[type=hidden]').val('1');
            $(this).closest('fieldset').hide();
            return event.preventDefault();
        });
        $(document).on('click', '.add_fields', function (event) {
            var regexp, time;
            time = new Date().getTime();
            regexp = new RegExp($(this).data('id'), 'g');
            $(this).before($(this).data('fields').replace(regexp, time));
            $(document).find('.date-of-service').datepicker({
                dateFormat: 'yy-mm-dd'
            });

            return event.preventDefault();
        });
    };

    $(document).ready(ready);

    $(document).on('page:load', ready);

}).call(this);

应用程序助手.rb

def link_to_add_fields(name, f, association, **args)
    new_object = f.object.send(association).klass.new
    id = new_object.object_id
    fields = f.fields_for(association, new_object, child_index: id) do |ff|
      render(association.to_s.singularize + "_fields", f: ff)
    end
    link_to(name, '#', class: "add_fields" , data: {id: id, fields: fields.gsub("\n", "")})
end

我们的表格

<%= f.fields_for :lineitems do |ff| %>

  <%= render 'lineitem_fields', f: ff %>
添加另一个发票行项目'.html_safe, f, :lineitems%>

我们添加动态的部分

<%= f.fields_for :items do |ff| %>

  <%= render 'items_fields', f: ff %>
添加另一个项目 item'.html_safe, f, :items%>

标签: ruby-on-railsrubynested-attributes

解决方案


推荐阅读