gpt4 book ai didi

ruby-on-rails - 具有 has_one 关系的两级深度嵌套形式中的不允许参数

转载 作者:数据小太阳 更新时间:2023-10-29 08:44:35 30 4
gpt4 key购买 nike

虽然我一直在获取未经许可的属性错误,但我已经在考试 Controller 中允许了这些属性。

第一层嵌套工作正常。第二级,答案不保存,服务器说“unpermitted parameters: answers”

我们将不胜感激任何帮助

模型exam.rb

 class Exam < ActiveRecord::Base

mount_uploader :attachment, PdfUploader #mount the pdf uploader

validates_presence_of :title, :date, :unit

belongs_to :unit

has_many :questions, :dependent => :destroy

accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:question].blank? }, :allow_destroy => true
end

问题模型question.rb

class Question < ActiveRecord::Base
belongs_to :exam

has_one :answer, :dependent => :destroy

accepts_nested_attributes_for :answer#, :reject_if => lambda { |a| a[:answer].blank? }, :allow_destroy => true
end

答案模型answers.rb

class Answer < ActiveRecord::Base
belongs_to :question
end

exams_controller.rb

 def new
@exam = Exam.new
2.times do
question = @exam.questions.build()
1.times{ question.build_answer }
end

结束

def exam_params
params.require(:exam).permit(:title, :attachment, :date, :unit_id,
questions_attributes:[ :id, :question, :exam_id, :_destroy,
answer_attributes:[:id, :answer, :question_id, :_destroy]]
)
end

_form.html.haml

    .field
= f.label :Exam_Title
= f.text_field :title , size: 100
.field
= f.label :date
= f.datetime_select :date
.field
= f.fields_for :questions do |builder|
=render "questions/question_fields", :f => builder

question_fields.html.haml 部分

%br/
= f.label :question, "Question"
%br/
= f.text_area :question
%br/
= f.check_box :_destroy
= f.label :_destroy, "Remove Question"

= f.fields_for :answers, @question.answer do |builder|
=render "answers/answer_fields", :f => builder

answer_fields.html.haml 部分内容

%br/
= f.label :answer, "Answer"
= f.text_field :answer
= f.check_box :_destroy
= f.label :_destroy, "Remove Answer"

服务器响应

 Processing by ExamsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Wns/Q8BAnzge2swUgCmY7yOex3CfUBkViUmiyp7enYMTbeMi+orYS9v7Brqrn7+eTenkaMl9H69vYCt2iqmVcg==", "exam"=>{"title"=>"Now with Answers", "date(1i)"=>"2015", "date(2i)"=>"11", "date(3i)"=>"5", "date(4i)"=>"12", "date(5i)"=>"41", "questions_attributes"=>{"0"=>{"question"=>"What is this?", "_destroy"=>"0", "answers"=>{"answer"=>"This is what", "_destroy"=>"0"}, "id"=>"22"}, "1"=>{"question"=>"What is that?", "_destroy"=>"0", "answers"=>{"answer"=>"That is that!", "_destroy"=>"0"}, "id"=>"23"}}, "unit_id"=>"4"}, "commit"=>"Save", "id"=>"14"}
Exam Load (0.3ms) SELECT "exams".* FROM "exams" WHERE "exams"."id" = $1 LIMIT 1 [["id", 14]]
Unpermitted parameter: answers
Unpermitted parameter: answers
(0.1ms) BEGIN
Question Load (0.2ms) SELECT "questions".* FROM "questions" WHERE "questions"."exam_id" = $1 AND "questions"."id" IN (22, 23) [["exam_id", 14]]
Unit Load (0.3ms) SELECT "units".* FROM "units" WHERE "units"."id" = $1 LIMIT 1 [["id", 4]]
(0.2ms) COMMIT

更新

改变:

def exam_params
params.require(:exam).permit(:title, :attachment, :date, :unit_id,
questions_attributes:[ :id, :question, :exam_id, :_destroy,
answers_attributes:[:id, :answer, :question_id, :_destroy]]
)
end

收件人:

def exam_params
params.require(:exam).permit(:title, :attachment, :date, :unit_id,
questions_attributes:[ :id, :question, :exam_id, :_destroy,
answer:[:id, :answer, :question_id, :_destroy]]
)
end

还是不行。同时将其更改为

def exam_params
params.require(:exam).permit(:title, :attachment, :date, :unit_id,
questions_attributes:[ :id, :question, :exam_id, :_destroy,
answers:[:id, :answer, :question_id, :_destroy]]
)
end

返回错误:

ActiveRecord::UnknownAttributeError (unknown attribute 'answers' for Question.):

在我的 Rails 控制台上:

 question.answer


=> #<Answer id: nil, answer: nil, created_at: nil, updated_at: nil, question_id: 21>

还是不明白是怎么回事。请帮帮我。

为表单呈现的 HTML:

 <div class='field'>
<br>
<label for="exam_questions_attributes_0_question">Question</label>
<br>
<textarea name="exam[questions_attributes][0][question]" id="exam_questions_attributes_0_question">
What is this?</textarea>
<br>
<input name="exam[questions_attributes][0][_destroy]" type="hidden" value="0" /><input type="checkbox" value="1" name="exam[questions_attributes][0][_destroy]" id="exam_questions_attributes_0__destroy" />
<label for="exam_questions_attributes_0__destroy">Remove Question</label>
<br>
<label for="exam_questions_attributes_0_answers_answer">Answer</label>
<input type="text" name="exam[questions_attributes][0][answers][answer]" id="exam_questions_attributes_0_answers_answer" />
<input name="exam[questions_attributes][0][answers][_destroy]" type="hidden" value="0" /><input type="checkbox" value="1" name="exam[questions_attributes][0][answers][_destroy]" id="exam_questions_attributes_0_answers__destroy" />
<label for="exam_questions_attributes_0_answers__destroy">Remove Answer</label>
<input type="hidden" value="22" name="exam[questions_attributes][0][id]" id="exam_questions_attributes_0_id" /><br>
<label for="exam_questions_attributes_1_question">Question</label>
<br>
<textarea name="exam[questions_attributes][1][question]" id="exam_questions_attributes_1_question">
What is that?</textarea>
<br>
<input name="exam[questions_attributes][1][_destroy]" type="hidden" value="0" /><input type="checkbox" value="1" name="exam[questions_attributes][1][_destroy]" id="exam_questions_attributes_1__destroy" />
<label for="exam_questions_attributes_1__destroy">Remove Question</label>
<br>
<label for="exam_questions_attributes_1_answers_answer">Answer</label>
<input type="text" name="exam[questions_attributes][1][answers][answer]" id="exam_questions_attributes_1_answers_answer" />
<input name="exam[questions_attributes][1][answers][_destroy]" type="hidden" value="0" /><input type="checkbox" value="1" name="exam[questions_attributes][1][answers][_destroy]" id="exam_questions_attributes_1_answers__destroy" />
<label for="exam_questions_attributes_1_answers__destroy">Remove Answer</label>
<input type="hidden" value="23" name="exam[questions_attributes][1][id]" id="exam_questions_attributes_1_id" />
</div>
<div class='field'>
<label for="exam_unit">Unit</label>
<select name="exam[unit_id]" id="exam_unit_id"><option value="1">Introduction to Comp Science</option>
<option value="2">Human Computer Interaction</option>
<option value="3">Management Information Systems</option>
<option selected="selected" value="4">Management Information Systems II</option></select>
</div>
<div class='actions'>
<input type="submit" name="commit" value="Save" />
</div>

最佳答案

我想我已经解决了。问题是,我必须添加编辑我的 exam.rb 模型,使其看起来像这样:

belongs_to :unit

has_many :questions, :dependent => :destroy

has_many :answers, :through => :questions

accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:question].blank? }, :allow_destroy => true

注意:我创建了一个名为 response 的新模型,其字段与 answer 完全相同,并按照建议编辑了我的 exam_controller.br通过 Rich Peck

似乎将行 has_many :answers, :through => :questions 添加到我的父模型中解决了这个问题。现在 parameter response 更改为 response_attributes 应该是这样。

谢谢,Rich Peck真的很有帮助。

关于ruby-on-rails - 具有 has_one 关系的两级深度嵌套形式中的不允许参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33547212/

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