Dhaval Chheda
Dhaval Chheda

Reputation: 5167

create permitted attributes for my form post

I have the following output from my form post and I want to create permitted attributes but I am not able to get it to work and a little confused.

POST data:-

Parameters: {"utf8"=>"✓", "authenticity_token"=>"nUAxX0FRhqhS+u8zTUcvogtHf8KPhcriNK95zLL0mAwd+/CyUrq80+wdq68c/h2MrBWvvTdBYwHv3IKH29ZcWQ==", "quiz"=>{"name"=>"Questions Testing", "questions_attributes"=>{"0"=>{"question_name"=>"Question # 1", "answer1"=>"", "answer2"=>"", "answer3"=>"", "answer4"=>"", "correct_answer"=>""}, "1"=>{"question_name"=>"", "answer1"=>"", "answer2"=>"", "answer3"=>"", "answer4"=>"", "correct_answer"=>""}}}, "commit"=>"Create Quiz"}

error:- Unpermitted parameter: questions_attributes

What I have tried is

params.require(:quiz).permit(:name, questions_attributes: [{0: [:question_name, :answer1, :answer2, :answer3, :answer4]}])

And I am not able to understand , how to get this to work so any help will be highly appreciated

Upvotes: 0

Views: 159

Answers (1)

Aakanksha
Aakanksha

Reputation: 976

I hope your association is something like

Quiz has_many Questions

and you're using accepts_nested_attributes_for :questions in your quiz.rb

If the above is correct, Use

params.require(:quiz).permit(:name, questions_attributes: [:question_name, :answer1, :answer2, :answer3, :answer4])

This should work as rails will handle it. Also refer this article for understanding the way rails works for nested attributes. It explains the same in a very simple manner.

If the error persists, please post it and i'll try to help better.

Upvotes: 1

Related Questions