Home create permitted attributes for my form post
Reply: 1

create permitted attributes for my form post

Dhaval Chheda
1#
Dhaval Chheda Published in 2017-11-07 07:13:07Z

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

Aakanksha
2#
Aakanksha Reply to 2017-11-07 07:22:27Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30101 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO