【问题标题】:Issue of deleting record in Nested Model Form Rails 4在嵌套模型表单 Rails 4 中删除记录的问题
【发布时间】:2014-11-14 08:52:41
【问题描述】:

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

像这样: 模特:

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"}}

我做错的地方。请指导我。提前致谢。

【问题讨论】:

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


    【解决方案1】:

    :_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 的地方

    【讨论】:

    • @HetalKhunti,如果成功了,请检查答案是否已接受:)
    • 是的,我试过了,但它说你可以在 6 分钟后接受,所以.. 我肯定会这样做。抱歉不能投票,因为我只有 11 个代表。并且需要它 min.15 rep
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多