加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 4 collection_check_boxes,带有has_many

发布时间:2020-12-17 03:23:59 所属栏目:百科 来源:网络整理
导读:我正在尝试将类别与产品相关联. 到目前为止我实现它的方式是 Class Product has_many :categorizations has_many :categories,through: :categorizations . Class Categorization belongs_to :product belongs_to :category . Class Category has_many :cate
我正在尝试将类别与产品相关联.
到目前为止我实现它的方式是

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 to:category_ids并设置强参数

def product_params
  params.require(:product).permit(:title,:description,:price,:category_ids => [])
end

解决方法

由于这种关系是多对多的,因此给定的产品应该响应category_ids(复数),而不是category_id(单数).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读