Getting fields_for to work with has_many relationship

浪子不回头ぞ 提交于 2019-12-05 12:58:32

It turns out in Rails 3, I need to use <%= fields_for ... %> instead of <% fields_for ... %>.

Try adding the following to your Workout model:

attr_accessible :scores_attributes

accepts_nested_attributes_for :scores

If you want to make sure that a score doesn't get built unless is it valid, and that is can be destroyed through the relationship you can expand to:

attr_accessible :scores_attributes

accepts_nested_attributes_for :scores, reject_if: proc { |a| a[:field].blank? }, allow_destroy: true
validates_associated :scores

Just switch :field with a relevant field that is required for a score to be created.

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