2

雖然我已經獲取了不允許的屬性錯誤,但我允許在考試控制器中使用這些屬性。具有has_one關係的兩級深度嵌套窗體中的未經許可的參數

嵌套的第一級工作正常。第二個層次,答案不救,服務器說「不允許的參數:回答」

給予將高度appriciated

模型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 

UPDATE

發生變化:

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 

TO:

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> 

回答

2

我想我已經解決了它。問題是,我不得不添加編輯我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是真的很有幫助。

1

的問題是在這裏:

"answers"=>{"answer"=> 

這應該是"answers_attributes"=>{"answer"(就像它與questions)。

這樣做的原因是,您還沒有建立的answer對象在你的控制器(或至少做正確也無妨):

def new 
    @exam = Exam.new 
    2.times do 
    @exam.questions.build.build_answer 
    end 
end 

這應該得到它的工作。

+0

我仍然得到相同的錯誤。 **不允許的參數:答案** – Denn

+0

您是否更換過控制器?你能發佈你的HTML表單代碼嗎? –

+0

是的,我改變了它。更新它在qustion現在 – Denn