所以如果我有两个这样的模型:
So if I have a two models like this:
#parent.rb class Parent < ApplicationRecord has_many :children end #children.rb class Child < ApplicationRecord belongs_to :parent end您将如何创建允许您在创建父级的表单中创建多个子级的表单?
How would you create a form that allows you to create multiple children in the form that creates the parent?
推荐答案Cocoon[0] 很好地解决了这个问题,并且有一个很棒的示例应用程序.
Cocoon[0] solves this problem quite nicely, and has a great example app.
在后端滚动,在您的 Parent 模型上抛出 accepts_nested_attributes_for :children,执行一些 fields_for(或 simple_fields_for) 内容,并确保您可以通过将 children_attributes: [:name, :age] 添加到您的 parent_params 来分配属性.
Rolling on the back-end, throw accepts_nested_attributes_for :children on your Parent model, do some fields_for (or simple_fields_for) stuff in your form, and make sure you can assign the attributes by adding children_attributes: [:name, :age] to your parent_params.
[0] github/nathanvda/cocoon
更多推荐
从表单创建 has
发布评论