我有如下關聯:
變體 has_may 顏色和顏色 has_many 大小。我正在使用活動管理 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_colours 表單,但它不會在變體顏色下構建 variant_sizes 表單。構建意味著它不會填充表單(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
uj5u.com熱心網友回復:
可能不需要f.has_many用任何東西包裝,因此您可以嘗試洗掉一個或兩個嵌套的f.inputs并且color_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]
]
我的猜測是問題出在您允許的引數中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/364627.html
