這是我的模型:如何設置permit_params另一個模型
Product.rb:
class Product < ApplicationRecord
belongs_to :position
has_many :products_sizes
has_many :sizes, through: :products_sizes
has_many :reviews, dependent: :destroy
accepts_nested_attributes_for :products_sizes
end
Products_size.rb:
class ProductsSize < ApplicationRecord
belongs_to :product
belongs_to :size
has_many :prices
accepts_nested_attributes_for :prices
end
Size.rb :
class Size < ApplicationRecord
has_many :products_sizes
has_many :products, through: :products_sizes
end
和Price.rb:
class Price < ApplicationRecord
belongs_to :products_size
end
在ActiveAdmin我需要爲產品的形式,當我更新的產品,我可以創造一個價格,所以表格外觀的一部分像這樣:
... #here is the begining of the form
f.inputs 'Sizes' do
f.semantic_fields_for ProductsSize.where(product_id: params[:id], size_id: Product.find(params[:id]).products_sizes.size.to_i).first.prices.new do |ps|
ps.input :products_size_id, label: 'Size', as: :select, collection: Product.find(params[:id]).sizes.map { |s| ["#{s.title}", s.id] }
ps.input :quantity
ps.input :amount
li do
link_to 'Add size', '#'
end
end
end
這一切似乎都很好,除非單擊提交按鈕時,價格未創建。我想,那是因爲permit_params
未指定爲price
。我如何指定它們?謝謝。
是的,但我需要設置'permit_params'不是爲我的'Product'模型,而是'Price',它具有'belongs_to:products_size'關係。我通過'ProductsSize'模型達到'價格'。 –
你允許控制器中的參數嗎? –