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()  
                @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
---------------------------------------------------------------------------------^
-------------------------------------------------------------------------------------------------------------------------------------^
                        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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With