Drop-Down-Menu for Many-to-Many relation in rails using nested attributes

前端 未结 2 616
小鲜肉
小鲜肉 2021-01-03 15:09

I have three tables via many-to-many-association: Supermarket, Product and Supply. Each Supermarket can hold many products and each product can be sold in many supermarkets.

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 15:33

    In you products form you need to add this line...

    <%= collection_select(:product, :supermarket_ids, SuperMarket.all, :id, :name, {}, { :multiple => true } )%>
    

    You also shouldn't need to use an accepts_nested_attributes for this, the many to many association you already have set up should take care of the rest.

提交回复
热议问题