Rails 4集合_选择排序



我正试图按名称而不是默认ID来订购我的collection_select下拉列表。

我已经在Rails4中找到了实现这一点的方法,但还没有找到解决方案。我的代码是:

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

这仍然返回一个按ID排序的列表。我使用的型号是:

Glaz.rb
has_many:成分
has_many:材料,:through=>:成分

配料.rb
belongs_to:釉料
belongs_to:材料物料.rb
has_many:成分
has_many:釉料,:through=>:配料

试试这个,它应该会起作用。将传递name和带有c.id 的属性值

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

最新更新