gpt4 book ai didi

javascript - 嵌套 Rails 模型 - 在创建时忽略 child_index

转载 作者:数据小太阳 更新时间:2023-10-29 07:39:57 26 4
gpt4 key购买 nike

因此,我一直在使用代码在嵌套模型 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

View 和部分:

#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);
}

当我创建新模型时,我获得了所有正确的字段,但它们缺少子索引 - 它不是空的 ([]),它只是不存在。关于为什么会发生这种情况的任何想法?它与我的其他嵌套模型完美配合,当我在常规 fields_for 中打开 child_index 时,它似乎工作正常。

谢谢!

最佳答案

检查您在 javascript 中遍历的 DOM。

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

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

代替

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

实现与您的目标相似的结果。

我认为,通过调用 .prev 选择器,您将跳转到上一个 div,而且我认为这在 DOM 树上移动得太远,无法实现您想要实现的目标。

关于javascript - 嵌套 Rails 模型 - 在创建时忽略 child_index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7087184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com