【发布时间】:2016-08-04 10:01:50
【问题描述】:
这些是我的模型:
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
大小.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 指定。我该如何指定它们?谢谢。
【问题讨论】:
标签: ruby-on-rails associations activeadmin