Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpermitted attributes _destroy when deleting nested attribute

Following railscasts#196 but using Rails 4 - I'm running into a problem when attempting to delete an answer from a question parent model.

Unpermitted parameters: _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'm removing the fields from the form on click fine but the record has not been deleted.

Thanks

Edit: JS to set the hidden variable

 jQuery ->                                                                                                                                                                                                   
        $(document).on 'click' , ".remove_fields" , (event) ->                                                                                                                                              
                $(this).prev('input[type=hidden]').val('1')                                                                                                                                                 
                $(this).closest('fieldset').hide()                                                                                                                                                          
                event.preventDefault()  
like image 416
MikeW Avatar asked Jan 05 '14 13:01

MikeW


2 Answers

@Hitham S. AlQadheeb may be right – check that 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

the Unpermitted parameters: _destroy error however should actually be referring to your controller's implementation of Strong Params: survey_params. You need to tell rails that your form will be passing the :_destroy field. Try this:

def survey_params                                                                                                                                                                                       
  params.require(:survey).permit(:name, :questions_attributes => [:id, :content, :_destroy, :answers_attributes => [:id, :response, :_destroy]])                                                                              
end
---------------------------------------------------------------------------------^
-------------------------------------------------------------------------------------------------------------------------------------^
like image 153
msanteler Avatar answered Sep 21 '22 20:09

msanteler


It should be

question.rb

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

Look at it again http://railscasts.com/episodes/196-nested-model-form-part-1

like image 25
Sully Avatar answered Sep 17 '22 20:09

Sully