I have the following code in my models:
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
I have a form to create a new Farm, and I want to create farm_products along with this form. My farm_products table can not only contain foreign key fields. How can I add or remove nested columns via Javascript and/or JQuery?
UPD. I found an awesome gem by nested_forms that doing exactly what I want! Here is a code in my view
= 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
But got an error that says
undefined method `klass' for nil:NilClass
There is probably something wrong with my relationships, but i can't find the problem.