为表单帖子

时间:2017-11-07 07:13:08

标签: ruby-on-rails ruby-on-rails-4

我的表单帖子中有以下输出,我想创建允许的属性,但我无法让它工作并且有点困惑。

POST数据: -

  

参数:{" utf8" =>"✓",   " authenticity_token" = GT;" nUAxX0FRhqhS + u8zTUcvogtHf8KPhcriNK95zLL0mAwd + / + CyUrq80 wdq68c / h2MrBWvvTdBYwHv3IKH29ZcWQ ==&#34 ;,   "测验" => {"名称" =>"问题测试",   " questions_attributes" => {" 0" => {" question_name" =>"问题#1",   " answer1" =>""," answer2" =>""," answer3" => ;""," answer4" =>"",   " correct_answer" =>""}," 1" => {" question_name" =>"&# 34;," answer1" =>"",   " answer2" =>""," answer3" =>""," answer4" => ;""," correct_answer" =>""}}},   "提交" =>"创建测验"}

错误: - 未允许的参数:questions_attributes

我试过的是

  

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

而且我无法理解,如何让这个工作,所以任何帮助将受到高度赞赏

1 个答案:

答案 0 :(得分:1)

我希望您的关联类似

测验has_many问题

并且您在accepts_nested_attributes_for :questions

中使用了quiz.rb

如果上述内容正确无误,请使用

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

这应该可以像rails一样处理它。另请参阅this文章,了解rails对嵌套属性的工作方式。它以非常简单的方式解释了相同的内容。

如果错误仍然存​​在,请发布,我会尽力提供帮助。