删除嵌套模型表格Rails 4中的记录的问题

时间:2014-11-14 08:52:42

标签: ruby-on-rails ruby nested-forms railscasts

我正在关注RailsCast # 196 Nested Model Form Part 1。我给出了控制器和模型的相同名称以及它的所有属性。但现在当我尝试进行编辑并删除问题时。如果我选中该复选框,则不会删除该问题。

像这样: enter image description here型号:

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

调查控制器:

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

查看文件

<%= 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 %>

正如我在中止的更新方法中评论的那样。如果我这样做,我会中止

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

我错了。请指导我。提前谢谢。

1 个答案:

答案 0 :(得分:6)

:_destroy添加到允许的参数

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

另外,请确保排在allow_destroy: true,您可以在其中定义accepts_nested_attributes_for :questions