Rails 3.1+ Nested Forms Issue: Can't mass-assign protected attributes

半世苍凉 提交于 2019-11-28 14:24:28

The problem is with the way you are naming your nested association attribute, and with the parameters you are sending:

The attribute for nested :players should be :players_attributes, and :team_id should not be listed (because that overrides the team association rails magic):

attr_accessible :jersey_number, :class_year, :players_attributes

And the parameters should have this format:

"roster"=>{"players_attributes"=>[{"first_name"=>"first name", "last_name"=>"last name"}], "class_year"=>"freshman", "jersey_number"=>"23"}, "commit"=>"Add To Team", "id"=>"1"}

To accomplish this, you should probably change simple_form_for @roster to simple_nested_form_for @roster

Note that you don't need to include the "team_id" attribute in the parameters for "roster" because that you are already loading and associating the roster to it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!