3

我正在从模型中提取类别列表。在管理部分,我想用它来为产品分配类别。它工作正常,但列表按添加类别的顺序显示。我想按字母顺序对它们进行排序,但我无法确定。

我敢肯定这很简单(希望如此)

这是我的代码:

<%= simple_form_for(@game) do |f| %>
  <%= f.input :name %>
  <%= f.input :description %>
  <%= f.input :copy %>
  <%= f.input :image %>
  <%= f.input :thumbnail %>
  <%= f.input :heroimage %>
  <%= f.association :category, collection: @categories %>
  <%= f.button :submit %>
<% end %>

我试图添加一个.sort_by(desc)或只是.sort在收集方法上,但它不会更改列表。

干杯

4

2 回答 2

7

以下是您应该如何更新代码:

<%= f.association :category, collection: Category.order('name ASC') %>

这假设您要按类别名称按升序排序。

于 2013-06-04T21:32:13.097 回答
4

I imagine @categories is assigned as an arel in your controller, can you add an .order("description") to that; e.g.

@categories = Category.order('description')
于 2012-04-27T16:12:33.493 回答