【发布时间】: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