[英]Ruby on Rails: How to use a local variable in a collection_select
[英]Ruby on Rails: How to sort a collection_select
我想按數據庫表列“播放”對它進行排序/排序(我想要的方式是降序還是升序)我完全糊塗了。 剛剛找到了 select 但不是 collection_select 的解決方案?
我觀點的一些代碼
<%= f.collection_select :player1, Player.all, :id, :name %>
不知道如何排序/排序
數據庫表中還有一些列,例如“plays”、“goals”...
只需將實際排序的集合傳遞給 collection_select 助手:
collection_select(:post, :author_id, Author.order('created_at DESC'), :id, :name_with_initial, :prompt => true)
因此,在您的源示例中,它將如下所示:
<%= f.collection_select :player1, Player.order('plays DESC'), :id, :name %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.