2

我的 new.html.erb 工作正常。

但在我的 edit.html.erb 中,假设我有 3 个现有部门......

每当我单击添加部门链接(添加另一个字段)时,它都会继续在现有部门上方而不是在下方。

顺便说一句,我的协会说部门有很多部门,部门属于部门。

在我的控制器中:有我的新方法和编辑方法

def new
    @division = Division.new
     @division.departments.build 
  end

 def edit
    @division = Division.find(params[:id])

  end

我需要 cocoon js 文件,所以我使用默认值而不是覆盖其他内容。

茧.js

(function($) {

  function replace_in_content(content, regexp_str, with_str) {
    reg_exp = new RegExp(regexp_str);
    content.replace(reg_exp, with_str);
  }

  function trigger_removal_callback(node) {
    node.parent().parent().trigger('removal-callback');
  }

  $('.add_fields').live('click', function(e) {
    e.preventDefault();
    var $this                 = $(this),
        assoc                 = $this.data('association'),
        assocs                = $this.data('associations'),
        content               = $this.data('template'),
        insertionMethod       = $this.data('association-insertion-method') || $this.data('association-insertion-position') || 'before';
        insertionNode         = $this.data('association-insertion-node'),
        insertionCallback     = $this.data('insertion-callback'),
        removalCallback       = $this.data('removal-callback'),
        regexp_braced         = new RegExp('\\[new_' + assoc + '\\]', 'g'),
        regexp_underscord     = new RegExp('_new_' + assoc + '_', 'g'),
        new_id                = new Date().getTime(),
        newcontent_braced     = '[' + new_id + ']',
        newcontent_underscord = '_' + new_id + '_',
        new_content           = content.replace(regexp_braced, '[' + new_id + ']');

    if (new_content == content) {
        regexp_braced     = new RegExp('\\[new_' + assocs + '\\]', 'g');
        regexp_underscord = new RegExp('_new_' + assocs + '_', 'g');
        new_content       = content.replace(regexp_braced, '[' + new_id + ']');
    }

    new_content = new_content.replace(regexp_underscord, newcontent_underscord);

    if (insertionNode){
      insertionNode = insertionNode == "this" ? $this : $(insertionNode);
    } else {
      insertionNode = $this.parent();
    }

    var contentNode = $(new_content);

    // allow any of the jquery dom manipulation methods (after, before, append, prepend, etc)
    // to be called on the node.  allows the insertion node to be the parent of the inserted
    // code and doesn't force it to be a sibling like after/before does. default: 'before'
    insertionNode[insertionMethod](contentNode);

    $this.parent().trigger('insertion-callback');
  });

  $('.remove_fields.dynamic').live('click', function(e) {
    var $this = $(this);
    trigger_removal_callback($this);
    e.preventDefault();
    $this.closest(".nested-fields").remove();
  });

  $('.remove_fields.existing').live('click', function(e) {
    var $this = $(this);
    trigger_removal_callback($this);
    e.preventDefault();
    $this.prev("input[type=hidden]").val("1");
    $this.closest(".nested-fields").hide();
  });

})(jQuery);

在我的 _form.html.erb

<div class="new-div-box">
<%= simple_form_for [:admin, @division]  do |f| %>
  <%= f.input :name, :label => "Division Name"%>

<div>
<div>

  <%= f.simple_fields_for :departments do |department| %>
  <%= render 'department_fields', :f => department %>

  <% end %>
</div>
  <%=link_to_add_association "Add Department", f, :departments,class: "btn btn-inverse" %>
</div>

  <%= f.submit  :class=>"btn btn-primary submit_btn_division" %>

<% end %>

</div>

我渲染的是_department_fields.html.erb

<div class="nested-fields">
  <%= f.input :name, :label => "Department Name"  %>
  <%= link_to_remove_association raw("<img src='../../../assets/remove.png' width='25px' height='25px'>"), f %>
</div>

我想知道为什么新添加的字段高于我的 edit.html.erb 中的现有字段

4

2 回答 2

4

我认为它会寻找一个具有关联的 div 作为包装嵌套字段的 id。

所以你的看法

<div id="departments">
  <%= f.simple_fields_for :departments do |department| %>
    <%= render 'department_fields', :f => department %>
  <% end %>
  <div class="links">
    <%=link_to_add_association "Add Department", f, :departments,class: "btn btn-inverse" %>
  </div>
</div>
于 2013-03-06T16:01:50.177 回答
3

@spullen 的解决方案可行,但他的推理不正确。如果你通过 coccon 的代码进行追踪,你会遇到这样的事情

var getInsertionNodeElem = function(insertionNode, insertionTraversal, $this){
    if (!insertionNode){
      return $this.parent();
    }
    ...
}

然后我相信这var addedContent = insertionNodeElem[insertionMethod](contentNode);会添加到新节点中。

关键是新的部分将插入到link_to_add_association的父节点上方。Solink_to_add_association<a>元素和新输入字段的容器节点将是兄弟节点。

如果您希望插入的部分位于下方,则link_to_add_association必须将其包裹在自己的 div 中。例子:

  <div id="workout-fields">
    <%= f.simple_fields_for :workouts, validate: true do |workout| %>
      <%= render partial: 'shared/workout/form', locals: {f: workout, show_remove: false} %>
    <% end %>
    <div><%= link_to_add_association 'add workout', f, :workouts, partial: 'shared/workout/form'%></div> <!-- this wrapper is IMPORTANT for insertion ordering -->
  </div>
于 2017-08-17T19:55:23.703 回答