如何将数组传递给 Rails 中的 fields_for?
我想对关联中的记录子集使用 fields_for
。
我有一个 Month
模型,它 has_many : payments
。
但在我看来,在我的表单中,我只想获得其中一些付款的fields_for
。例如:
- fields_for @month.payments.large
这不起作用。
我可以将一组记录传递给 fields_for
,而不是通常的符号 (fields_for : payments
) 方法吗?
I want to use fields_for
on a subset of records in an association.
I have a Month
model, which has_many :payments
.
But in my form in my view I only want to have fields_for
some of those payments. For example:
- fields_for @month.payments.large
This doesn't work.
Can I pass a set of records to fields_for
, rather than the usual symbol (fields_for :payments
) approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
但是,您可以使用对象数组,而无需创建任何其他关联。例如,假设在您的控制器中您准备了一些 @large_ payments 数组,那么在视图中您可以执行以下操作:
这样,如果您有一个相当大的表单或多页表单,并且您不希望必须为要显示的每个组创建额外的关联,但您不必这样做。
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:
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.
您可以为大额付款添加额外的关联,例如:
之后您可以以常见的方式使用
fields_for
:我认为将此逻辑封装在模型端是比在视图中更好的方法。
You can add additional association for large payments, for example:
After that you can use
fields_for
in common way:I think to encapsulate this logic on a model side is a better approach then in the view.