我有模特 - 测试,问题和教师答案。
test.rb
class Question < ActiveRecord::Base
belongs_to :test
has_many :teacher_answers, dependent: :destroy
accepts_nested_attributes_for :teacher_answers
end
question.rb
class TeacherAnswer < ActiveRecord::Base
belongs_to :question
end
teacher_answer.rb
---
standard scaffold code
---
def test_params
params.require(:test).permit(:title,
questions_attributes: [:question_text, :test_id, teacher_answers_attributes: [:teacher_answer_text, :correct, :question_id]],)
end
和控制器 test_controller.rb
---
standard scaffold code
---
def question_params
params.require(:question).permit(:question_text, :test_id,
teacher_answers_attributes: [:teacher_answer_text, :correct, :question_id])
end
question_controller.rb
`<span class="my_tag">#tag</span>`
使用问题和答案创建新测试时,它会正确创建所有内容,但在更新时:
答案 0 :(得分:1)
您应该允许:id
:_destroy
中的test_params
和update
允许delete
和def test_params
params.require(:test).permit(:title, questions_attributes: [:id, :question_text, :test_id, :_destroy, teacher_answers_attributes: [:id, :teacher_answer_text, :correct, :question_id, :_destroy]])
end
正常工作。
allow_destroy: true
<强> 更新 强>
您还应为test.rb
和question.rb
class Test < ActiveRecord::Base
has_many :questions, dependent: :destroy
accepts_nested_attributes_for :questions, allow_destroy: true
end
class Question < ActiveRecord::Base
belongs_to :test
has_many :teacher_answers, dependent: :destroy
accepts_nested_attributes_for :teacher_answers, allow_destroy: true
end
{{1}}
答案 1 :(得分:0)