Invalid _destroy attributes when deleting a nested attribute

Following railscasts # 196, but using Rails 4 - I have a problem trying to remove the answer from the parent model of the question.

Parameters not listed: _destroy

_answer_fields.html.erb

<fieldset>                                                                                                                                                                                                  
    <%= f.text_field :response %>                                                                                                                                                                       
    <%= f.hidden_field :_destroy %>                                                                                                                                                                                                                                                                                                                                                                                 
    <%= link_to "remove" , "#", class: "remove_fields" %>                                                                                                                                               
</fieldset>      

question.rb

accepts_nested_attributes_for :questions, :allow_destroy => true   

surveys_controller.rb

def survey_params                                                                                                                                                                                       
  params.require(:survey).permit(:name, :questions_attributes => [:id, :content, :answers_attributes => [:id, :response]])                                                                              
end 

I remove the fields from the form on click, but the record is not deleted.

thanks

Edit: JS to set a hidden variable

 jQuery ->                                                                                                                                                                                                   
        $(document).on 'click' , ".remove_fields" , (event) ->                                                                                                                                              
                $(this).prev('input[type=hidden]').val('1')                                                                                                                                                 
                $(this).closest('fieldset').hide()                                                                                                                                                          
                event.preventDefault()  
+4
source share
2 answers

@Hitham S. AlQadheeb may be right - make sure your models are correct ...

survey.rb

class Survey < ActiveRecord::Base
  has_many :questions, :dependent => :destroy
  accepts_nested_attributes_for :questions
end

and question.rb

class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :answers, :dependent => :destroy
  accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

Unpermitted parameters: _destroy, , Strong Params: survey_params. , :_destroy. :

def survey_params                                                                                                                                                                                       
  params.require(:survey).permit(:name, :questions_attributes => [:id, :content, :_destroy, :answers_attributes => [:id, :response, :_destroy]])                                                                              
end
---------------------------------------------------------------------------------^
-------------------------------------------------------------------------------------------------------------------------------------^
+6

question.rb

belongs_to :survey 
has_many :answers, : dependent => :destroy
accepts_nested_attributes_for :answers, :allow_destroy => true

http://railscasts.com/episodes/196-nested-model-form-part-1

+1

All Articles