Nested Rails models - ignoring child_index on create
So, I've been using the code for adding a new model in the nested models Railscast. For whatever reason, the fields_for in the code to add a new field seems to be ignoring the child_index argument. It's driving me absolutely nuts because I'm using the same code elsewhere with a different model and it's working perfectly.
Models:
#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 and partial:
#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);
}
When I create the new model, I get all of the proper fields, but they are missing the child index - it's not empty ([]), it just isn't there. Any ideas for why this might be happening? It works perfectly with my other nested model, and 开发者_Python百科when I mess with child_index on in the regular fields_for, it seems to work alright.
Thanks!
Check your DOM traversing in your javascript.
In my use case, I have the following script:
$(this).before($(this).data('fields').replace(regexp, time))
instead of
$(link).parent().prev().append(html);
to achieve a similar outcome to what you are aiming for.
I think that by calling the .prev selector you are jumping up to the previous div and I think that this is moving too far up the DOM tree for what you are trying to achieve.
精彩评论