Drop down menu with value from another model

前端 未结 1 1205
抹茶落季
抹茶落季 2020-12-25 09:03

I have products belonging to collections. A collection is just a name. Products have a collection_id.

In my _form view that is used for creation and edition of produ

相关标签:
1条回答
  • 2020-12-25 09:22

    You can use a collection select, first make sure your models are properly setup:

    class Product
      belongs_to :collection
    end
    
    class Collection
      has_many :products
    end
    

    Then add the collection select to your view:

    <%= collection_select(:product, :collection_id, Collection.all, :id, :name) %>
    

    You can also read up on the documentation here.

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