RoR nested attributes produces duplicates when edit

孤街浪徒 提交于 2019-11-27 18:05:01

So I figured it out. I had to add :id to the permitted params in the survey_params method. It now looks like this:

# Never trust parameters from the scary internet, only allow the white list through.
def survey_params
  params.require(:survey).permit(:name, questions_attributes: [:id, :content])
end

which works perfectly. I'm a RoR newbie, so please take my analysis of this with a grain of salt, but I guess that new id's where generated instead of being passed to the update action. Hope this helps someone else out there.

Using cocoon gem on Rails 4, I was still getting duplicate fields even after adding :id to the permitted list when editing. Noticed the following as well

Unpermitted parameters: _destroy
Unpermitted parameters: _destroy

So I added the :_destroy field to the permitted model_attributes: field and things worked smoothly after that.

For example...

def survey_params
  params.require(:survey).permit(:name, questions_attributes: [:id, :content, :_destroy])
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!