我正在进行调查,遵循http://railscasts.com/episodes/196-nested-model-form-revised?view=asciicast
我可以通过accepts_nested_attributes_for
调查有很多问题,
问题有很多答案,
我想知道删除问题的逻辑是什么。
我只需要勾选方框并更新对象,它会删除模型中的问题对象吗?
为什么?我想我需要通过检查复选框是否被选中来执行控制器中的destroy方法?
我想知道,如果不编写OJBECT.destroy
代码
class Question < ActiveRecord::Base
has_many :answers
belongs_to :survey
accepts_nested_attributes_for :answers, allow_destroy: true
end
= f.check_box :_destroy
= f.label :_destroy, "Remove Question"
$('form').on 'click', '.remove_fields', (event) ->
$(this).prev('input[type=hidden]').val('1')
def survey_params
params.require(:survey).permit(
:title,
questions_attributes: [:id, :content, :survey_id, :_destroy,
answers_attributes:[:id, :content, :question_id, :_destroy]] ,
)
end
答案 0 :(得分:0)
在新的调查视图中,您只有表单来创建问题和答案,并在客户端动态添加。当您删除时,实际上您只是通过触发javascript来删除客户端中的表单。此时,模型的实例尚不存在,因此您不需要在控制器中执行销毁操作。
只有当您提交调查时,调查,问题和答案才会创建模型并保存到数据库。