Deep Nested Rails 4 Form

我们两清 提交于 2019-11-30 09:05:52

I think your problem is with your strong params:

def item_params
      params.require(:item).permit(:id, :content, :kind, questions_attributes: [:content, :helper_text, :kind, answers_attributes: [:content, :correct]])
end   

Basically, when you pass a deep nested form (where you have multiple dependent models), you'll have to pass the attributes as part of the other model's attributes. You had the params as separate

I run into a similar issue and, while Richard Peck answer helped me as well, there is one thing that it was missing for me.

If you are deep-nesting you need to specify the id of the parent of the nested item. In this case to create an answers you need to make questions id explicit with q.input :id, otherwise you will run into this error.

= simple_form_for(@item) do |f|
    = ...
    = f.simple_fields_for :questions do |q|
        = ...
        = q.input :id
        = q.simple_fields_for :answers do |a|
            = ...
    = f.submit
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!