嵌套的Rails模型——在创建时忽略child_index

Nested Rails models - ignoring child_index on create

本文关键字:child index 创建 Rails 模型 嵌套      更新时间:2023-09-26

因此,我一直在使用代码在嵌套模型Railscast中添加新模型。无论出于何种原因,代码中用于添加新字段的fields_for似乎忽略了child_index参数。这简直要把我逼疯了,因为我在其他地方用不同的模型使用相同的代码,但它工作得很好。

模型:

#models/gradebook_settings.rb
class GradebookSettings
include Mongoid::Document
  has_many :assignment_types
  accepts_nested_attributes_for :assignment_types, :allow_destroy => true
  field :weight_type, :type => String, :default => "equal_weight"
end
#models/assignment_type.rb
class AssignmentType
include Mongoid::Document
  has_many :assignments
  belongs_to :gradebook_settings, :class_name => "GradebookSettings"
  field :course_id, :type => Integer
  field :name, :type => String
  field :weight, :type => Integer
end

视图和局部:

#views/gradebook_settings/new.html.haml
=form_for @settings, :remote => true, :url => "/settings/#{@settings.id}/weight", :html => {:method => "put"} do |f|
  %div{:id => "assignment_types", :style => ""}
    =f.fields_for :assignment_types do |builder|
      =render "assignment_type_fields", :f => builder
  %div{:id => "weight_type"}
   %div{:id => "equal_weight"}
      =f.label :weight_type, "Equal Weight"
      =f.radio_button :weight_type, :equal_weight
    %div{:id => "no_weight"}
      =f.label :weight_type, "No Weight"
      =f.radio_button :weight_type, :no_weight
    %div{:id => "manual_weight"}
      =f.label :weight_type, "Manual Weight"
      =f.radio_button :weight_type, :manual_weight
    =link_to_add_fields "+ Add Type", f, :assignment_types
  %button{:type => "button", :class => "button", :id => "submit_weight"}="Submit"
  =close_openBox_button

#views/gradebook_settings/_assignment_type_fields.html.haml
%div{:class => "assignment_type"}
  =f.text_field :name, {:size => "30"}
  =f.text_field :weight, {:size => "3", :maxlength => "2", :float => "right"}
  =f.hidden_field :_destroy, {:class => "type_destroy_field"}
  %span{:class => "remove_button button"}=button_to_function "X", "remove_fields(this, 'assignment_type')"
#helpers/application_helper.rb
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|
    render(association.to_s.singularize + "_fields", :f => builder)
  end
  button_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')", :id => "add_range_button")
end
Javascript:

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

当我创建新模型时,我得到了所有适当的字段,但它们缺少子索引-它不是空的([]),它只是不存在。你知道为什么会这样吗?它与我的其他嵌套模型完美地工作,当我在常规字段ds_for中混淆child_index on时,它似乎工作得很好。

谢谢!

检查javascript中的DOM遍历。

在我的用例中,我有以下脚本:

$(this).before($(this).data('fields').replace(regexp, time))

代替

$(link).parent().prev().append(html);

达到与你的目标相似的结果。

我认为通过调用。prev选择器你会跳到上一个div我认为这在DOM树中移动得太远了对于你想要实现的目标