Nested attributes not being destroyed after an update validation fails in Rails -


i'm struggling head around myself hope can explain well. have form nested models. simplified version of things follows (using rails 3.0.13);

#parent.rb  class parent < activerecord::base   has_many :children, :dependent => destroy   accepts_nested_attributes_for :children, :allow_destroy => true   validates_presence_of :thing   validate :children_unique    def children_unique     errors[:base] << "you have same child listed more once!" if self.children.map{|x| [child.name, child.age]} != self.children.map{|x| [child.name, child.age]}.uniq   end end   #child.rb  class child < activerecord::base   belongs_to :parents end  #parents_controller.rb  class parentscontroller < applicationcontroller   load_and_authorize_resource :parent   #using cancan authorization. calls @parent = parent.find(params[:id]); authorize! :update, @parent; @ start of update method    def update     if @parent.update_attributes(params[:operation])       redirect_to @parent.admission, :notice => 'operation updated successfully'     else       flash.now[:warning] = "parent not updated!"       render :action => "edit"     end   end end 

all pretty standard far. form set in pretty standard way well. call parent_form.fields_for :children , render partial children within fields_for block. each child partial form contains delete link , when clicked javascript used set hidden field attribute of _destroy set "1" , partial hidden view.

this works in instances odd problem have found follows;

if editing existing parent has, say, 2 children, , delete 1 of children , set 'thing' blank form fails validation expected (because 'thing' not present) , edit view re-rendered. in view results, child deleted present again! hidden _destroy field set "true" , if fill out 'thing' again , submit form, updated parent has 1 child.

i dealt adding conditional style tag nested child div <div class='fields'<%= " style='display: none;'" if f.object._destroy %>> no longer appear if deleted before attempting update record. achieves aim but, if , submit form without correcting empty 'thing' field model fails validation again, , in next edit form appears add new child identical 1 deleted , fill out 'thing' field, model fails children_unique validation though first of identical children has _delete attribute set "true".

clearly have tied myself in knots here , have tried vast array of alternative approaches , modifications , best have come far. it's very close still have weird edge case never happen in practice suggests don't quite understand way controller , models interacting.

can set me straight?

you should make delete link ajax call controller deletes child right away , removes it's markup page, way, if validation fails have removed , none of these 2 issues happen.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -