Rails具有活动管理的双嵌套形式



我有如下关联:

变体有多种颜色,颜色有多种尺寸。我正在使用主动管理gem来管理后端活动。我的模型看起来像

class Variant < ApplicationRecord
has_many :variant_colours, dependent: :destroy
accepts_nested_attributes_for : variant_colours, allow_destroy: true
end
class VariantColor < ApplicationRecord
belongs_to :variant
has_many :variant_sizes, dependent: :destroy
accepts_nested_attributes_for :variant_sizes, allow_destroy: true
end
class VariantSize < ApplicationRecord
belongs_to :variant_color
end

它正在构建具有给定字段的variant_colors表单,但不是在变体颜色下构建variant_size表单。构建意味着它不会填充表单(UI(上的字段

form do |f|
f.inputs do
f.input :name
f.input :product
f.input :sku
f.input :stock_quantity
f.inputs do
f.has_many :variant_colors, heading: 'Variant Colors',
allow_destroy: true,
new_record: true do |color_form|
color_form.input :color
color_form.input :sku_code
color_form.input :stock
color_form.inputs do
color_form.has_many :variant_sizes, heading: 'Variant Sizes',
allow_destroy: true,
new_record: true do |size_form|
size_form.input :size
size_form.input :sku_code
size_form.input :stock
end
end
end
end
end
f.actions
end

可能不需要用任何东西包装f.has_many,所以您可以尝试删除嵌套的f.inputscolor_form.inputs中的一个或两个,它们是您在表单中包装has_many输入块的。

我的下一个想法是,你是如何在控制器中声明允许的参数的?它们可能需要像一样

permit_params :name, :product, :sku, :stock_quantity, 
variant_colors_attributes: [
:color, :sku_code, :stock, :_destroy, 
# I don't think I've ever tried to double nest in this way, you may need diff syntax
variant_sizes_attributes: [:size, :sku_code, :stock, :_destroy]
]

我猜问题出在你允许的情人身上。

最新更新