簡體   English   中英

Rails 4 collection_select訂購

[英]Rails 4 collection_select ordering

我正在嘗試按名稱而不是默認ID排序我的collection_select下拉列表。

我在Rails 4中搜索了一種方法來執行此操作,但尚未找到解決方案。 我的代碼是:

<%= f.collection_select(:material_id, Material.order('name ASC').all, :id, :name) %>

這仍然返回按ID排序的列表。 我使用的模型是:

釉面
has_many:成分
has_many:materials,:through =>:ingredients

Ingredients.rb
當屬:釉
所屬:材料

Material.rb
has_many:成分
has_many:glazes,:through =>:ingredients

試試這個,它應該起作用。 將通過c.id傳遞name和屬性值

<%= f.select :material_id, Material.order('name ASC').map {|c| [c.name, c.id] }, 
                           {prompt:"Choose Material"}, class: "form-control" %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM