Expected ProductField, got array issue

妖精的绣舞 提交于 2019-12-05 04:56:33

Make sure your Product and Store models have:

accepts_nested_attributes_for

inside them.

Then, if your calling nested fields_for like that, make sure you build them (in the controller), something like:

product = @store.products.build
product.productFields.build

Firstly you should have set accepts_nested_attributes_for in your models like this

class Store < ActiveRecord::Base
  has_many :products
  accepts_nested_attributes_for :products
end

class Product < ActiveRecord::Base
  has_many :product_fields
  belongs_to :store
  accepts_nested_attributes_for :product_fields
end

class ProductField < ActiveRecord::Base
  belongs_to :products
end

Secondly, your store_params should look like this

def store_params
  params.require(:store).permit(:name, :description, :user_id, products_attributes: [:id, :type, { product_fields_attributes: [:type, :content ] } ])
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!