gpt4 book ai didi

ruby-on-rails - Rails fields_for 里面的 fields_for

转载 作者:太空宇宙 更新时间:2023-11-03 18:24:29 25 4
gpt4 key购买 nike

我怎样才能实现这样的目标?
结构是 => 一个测试有很多问题,一个问题有很多答案。

我在 Controller 中有 questions = @test.questions.buildquestions.answers.build

form_for @test do |f|
f.fields_for :questions do |question_f|
question_f.fields_for :answers do |answer_f|
# answer form here

它一直工作到 fields_for :answers。

我错过了什么?谢谢!

最佳答案

如果您想使用嵌套形式,您还应该将 accepts_nested_attributes_for 放入您的测试和问题模型中:

class Test < ActiveRecord::Base
attr_accessible :questions_attributes
has_many :questions, dependent: :destroy
accepts_nested_attributes_for :questions
end

class Question < ActiveRecord::Base
attr_accessible :answers_attributes
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers
end

试试这个:

form_for ([@test, @question]) do |f|

在 Controller 中的新操作中:

@test = Test.new
@question = Question.new
@test.questions.build
@question.answers.build

关于ruby-on-rails - Rails fields_for 里面的 fields_for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13225965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com