嵌套的Rails模型——在创建时忽略child_index
Nested Rails models - ignoring child_index on create
因此,我一直在使用代码在嵌套模型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树中移动得太远了对于你想要实现的目标
相关文章:
- Emberjs应用程序加载在除Index之外的所有路由上
- 分析高度属性时出现意外值{{specs.height}}.index.html
- 主干模板:index.jst.eco到index.jst.ejs
- 将 jQuery 代码添加到 Index.aspx 页面
- AngularJS-需要在index.html页面中访问来自服务的数据
- 更改基于它的Select OPTION's INDEX(Jquery)
- 尝试创建一个具有z-index的堆叠菜单'使用HTML5/CSS3/JS
- 在通过child.print()打印之前,我如何等待图像加载到我的新窗口中
- index.html使用webpack开发服务器未重新加载
- 从index.html调用函数,该函数无限循环
- RouteChangeStart事件在初始index.html页面加载时触发
- Is (!~index) faster than (index === -1)?
- KOValidation在错误消息中获取可观察值、$index()、$data等
- 为什么我可以'不要使用xx[this.index].style.display?它's说“;无法读取属性
- Angular2:Index.html没有't显示该文件;正在加载&”;
- 如何使用gulp-inject将文件的内容插入到我的index.html中
- 如何在ng repeat angular js中使用$index
- 使用index.ts文件导出类会导致注入的构造函数中未定义
- 使url匹配'/''/索引'和'/index.html'到单一状态
- AngularJS:如何在ng repeat中使用$index