How do I pass an array to fields_for in Rails?

前端 未结 2 1909
离开以前
离开以前 2021-01-13 14:22

I want to use fields_for on a subset of records in an association.

I have a Month model, which has_many :payments.

But

2条回答
  •  生来不讨喜
    2021-01-13 15:12

    You can, however, use an array of objects without having to create any additional associations. For example, let's say that in your controller you prepared some array of @large_payments, then in the view you can do the following:

    <%= f.fields_for :payments, @large_payments do |payment| %> ...
    

    That way if you've got a pretty big form or multiple pages of forms, and you don't want to have to create an additional association for each group that you want to display, you don't have to.

提交回复
热议问题