Rails 4 - Unpermitted Paramaters - Deeply nested attributes form not saving -
i'm working on nested attributes , running unpermitted attributes error.
log:
started post "/videos/1/quizzes" processing quizzescontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"xdj5lpz7quvezivybphnv0t/npmmtkval2dcwrxhvqu=", "quiz"=>{"name"=>"test", "question"=>{"content"=>"test", "answer"=>{"content"=>"test"}}}, "commit"=>"submit", "video_id"=>"1"} video load (0.1ms) select "videos".* "videos" "videos"."id" = ? limit 1 [["id", 1]] unpermitted parameters: question
so data being submitted. reading around looks nested question parameters supposed "question_attributes"=> in log shows "question"=>. don't know if has it? @ point it's idea.
quizzes#new:
def new @video = video.find(params[:video_id]) @quiz = @video.build_quiz @quiz.questions.build @quiz.questions.each |question| question.answers.build end end
the video_id params working , can see above being submitted.
quizzes#create
def create @video = video.find(params[:video_id]) @quiz = @video.create_quiz(quiz_params) respond_to |format| if @quiz.save format.html { redirect_to @quiz, notice: 'quiz created.' } format.json { render :show, status: :created, location: @quiz } else format.html { render :new } format.json { render json: @quiz.errors, status: :unprocessable_entity } end end end
quizzes_params
def quiz_params params.require(:quiz).permit(:name, questions_attributes: [:id, :content, :quiz_id, answers_attributes: [:id, :content, :question_id]]) end
so can see it's stopping question parameters , answer parameters being saved.
let me know if need else! thank in advance help!
update here form partial! tried doing form_for @quiz |f|
didn't change submitted. other thing tried pluarlizing :question
, :answer
:questions
, :answers
no luck.
<%= form_for [@video, @quiz] |f| %> <%= f.label :name, "title" %> <%= f.text_field :name %> <%= f.fields_for :question |questions| %> <%= questions.label :content, "question" %><br> <%= questions.text_area :content, rows: 3 %> <%= questions.fields_for :answer |answers| %> <%= answers.label :content, "answer" %> <%= answers.text_field :content %> <% end %> <% end %> <%= f.submit "submit" %> <% end %>
solution:
i changed line in quizzes_controller @quiz.questions.build
2.times { @quiz.questions.build }
, fixed issue. not sure why works now, does. explanation why appreciated!
def new @video = video.find(params[:video_id]) @quiz = @video.build_quiz 2.times { @quiz.questions.build } @quiz.questions.each |question| question.answers.build end end
Comments
Post a Comment