这是我的模型:如何设置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'模型达到'价格'。 –
你允许控制器中的参数吗? –