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

前端 未结 2 615
小鲜肉
小鲜肉 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.

    0 讨论(0)
  • 2021-01-03 15:45

    I think in views

    <%= f.collection_select "super_market_ids[]",@super_markets,:id,:name,{},{:multiple=>"multiple'} %>

    I am not sure about super_market_ids or super_market_ids[] and syntax just verified once.

    In select tag if you want checkbox type multi select there is an chosen library that will help you to build nicer UI,

    0 讨论(0)
提交回复
热议问题