Error deleting record in Rails Rails 4

I am following RailsCast # 196 Nested Model Form Part 1

. I gave the same name for the controllers as well as the model and all the attributes. But now when I try to go to edit and delete the question. It doesn't remove the question if I check the box.

Like this: enter image description hereModel:

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

      

Shooting 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 commented out the update method to cancel. If I interrupt this way, 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 am I going wrong. Please guide me. Thanks in advance.

+3


source to share


1 answer


Add :_destroy

to allowed options

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 the line where you defineaccepts_nested_attributes_for :questions

+6


source







All Articles