【发布时间】:2014-09-22 18:28:23
【问题描述】:
我正在尝试将类别与产品相关联。 到目前为止我实现它的方式是
Class Product
has_many :categorizations
has_many :categories, through: :categorizations
。
Class Categorization
belongs_to :product
belongs_to :category
.
Class Category
has_many :categorizations
has_many :products, through: :categorizations
在我的产品/_form.html.erb
<div class="field">
<%= f.label :category_id %><br />
<%= collection_check_boxes(:product, :category_id, Category.all, :id, :name) %>
</div>
我不确定如何正确执行此操作。
解决方案
更改::category_id 为 :category_ids 并设置强参数
def product_params
params.require(:product).permit(:title, :description, :price, :category_ids => [])
end
【问题讨论】:
标签: ruby-on-rails has-many-through checkboxlist categorization