gpt4 book ai didi

jquery - 有_许多 :through nested_form that can build multiple instances

转载 作者:行者123 更新时间:2023-12-01 00:04:34 25 4
gpt4 key购买 nike

我的模型中有以下代码:

Class Farm < ActiveRecord::Base
has_many :farm_products, :dependent => :destroy
has_many :products, :through => :farm_products
accepts_nested_attributes_for :farm_products
end

class Product < ActiveRecord::Base
has_many :farm_products, :dependent => :destroy
has_many :farms, :through => :farm_products
end

class FarmProduct < ActiveRecord::Base
belongs_to :farm
belongs_to :product
end

我有一个用于创建新农场的表单,我想随此表单一起创建 farm_products。我的 farm_products 表不仅可以包含外键字段。如何通过 Javascript 和/或 JQuery 添加或删除嵌套列?

UPD。我发现了nested_forms 的一个很棒的 gem ,它完全符合我的要求!这是我认为的代码

= nested_form_for @farm, :html => { :multipart => true } do |f|
= f.fields_for :farm_products do |fp|
-#fields goes here
= fp.link_to_remove 'Remove this task'
= fp.link_to_add "Add a task", :farm_products

但出现错误,提示

undefined method `klass' for nil:NilClass

我的人际关系可能有问题,但我找不到问题所在。

最佳答案

link_to_add 需要位于 fields_for block 之外,在 f 对象上调用,而不是在 fp 上调用对象。

关于jquery - 有_许多 :through nested_form that can build multiple instances,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5746006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com