I keep Getting unpermitted attributes error though, I have permitted the attributes in the exam controller.
The first level of nesting works fine. The second level, the answers don't save, the server says "unpermitted parameters: answers"
Any help accorded will be highly appriciated
The model 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
The Question Model 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
The answers model 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
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 partial
%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 partial
%br/
= f.label :answer, "Answer"
= f.text_field :answer
= f.check_box :_destroy
= f.label :_destroy, "Remove Answer"
The server response
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
Changing:
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
Still doesn't work. While changing it to
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
Returns an error:
ActiveRecord::UnknownAttributeError (unknown attribute 'answers' for Question.):
On my Rails console:
question.answer
=> #<Answer id: nil, answer: nil, created_at: nil, updated_at: nil, question_id: 21>
Still don't understand what is going on. Please help me out.
Rendered HTML for for the form:
<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>
I Think I've solved it. The issue was, I had to add edit my exam.rb
model to look like this:
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
Note: I created a new model called response
with exactly the same fields as answer
as well as edited my exam_controller.br
as advised by Rich Peck
It appears that adding the line has_many :answers, :through => :questions
to my parent model solved the issue.
Now the parameter response
changed to response_attributes
as it should be.
Thanks, Rich Peck was really helpful.