Issue of deleting record in Nested Model Form Rails 4

1.8k views Asked by At

I am following RailsCast # 196 Nested Model Form Part 1. I have given the same name of controllers as well as model and it's all attributes. But now when I try to go in edit and remove the question. It doesn't delete the question if I select the checkbox.

Like this: enter image description here Model:

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

class Question < ActiveRecord::Base
  belongs_to :survey
end

Survey Controller:

class SurveysController < ApplicationController
  before_action :set_survey, only: [:show, :edit, :update, :destroy]

  def index
    @surveys = Survey.all
  end

  def show
  end

  def new
    @survey = Survey.new
    3.times {@survey.questions.build}
  end

  def edit
  end

  def create
    @survey = Survey.new(survey_params)

    respond_to do |format|
      if @survey.save
        format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
        format.json { render :show, status: :created, location: @survey }
      else
        format.html { render :new }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    #abort params[:survey][:questions_attributes].inspect
    respond_to do |format|
      if @survey.update(survey_params)
        format.html { redirect_to @survey, notice: 'Survey was successfully updated.' }
        format.json { render :show, status: :ok, location: @survey }
      else
        format.html { render :edit }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @survey.destroy
    respond_to do |format|
      format.html { redirect_to surveys_url, notice: 'Survey was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_survey
      @survey = Survey.find(params[:id])
    end

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

View File

<%= form_for(@survey) do |f| %>
  <% if @survey.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved:</h2>

      <ul>
      <% @survey.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
    <%= f.fields_for :questions do |builder| %>

      <div class="field">
        <%= builder.label :content, 'Question' %><br>
        <%= builder.text_area :content, :rows=>3 %><br>
        <%= builder.check_box :_destroy %>
        <%= builder.label :_destroy, "Remove Question" %>
      </div>

   <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And As I have commented in update method for abort. If that way I do abort I get

{"0"=>{"content"=>"SEM 1", "_destroy"=>"0", "id"=>"4"}, "1"=>{"content"=>"Sem 2", "_destroy"=>"0", "id"=>"5"}, "2"=>{"content"=>"Sem 3", "_destroy"=>"1", "id"=>"6"}}

Where I do mistake. Please guide me. Thanks in advance.

1

There are 1 answers

2
Andrey Deineko On BEST ANSWER

Add :_destroy to permitted params

def survey_params
  params.require(:survey).permit(
    :name,
    questions_attributes: %i(
      id
      content
      _destroy
      survey_id
    )
  )
end

Also, make sure you have allow_destroy: true in line, where you define accepts_nested_attributes_for :questions