简体   繁体   English

如何在activeadmin rails的过滤器下拉列表中添加已排序的元素

[英]how to add sorted elements in the filter dropdown in activeadmin rails

I have a Post model, PostSource model. 我有一个Post模型,PostSource模型。 A PostSource has many posts, a post belong to one PostSource. PostSource有很多帖子,帖子属于一个PostSource。

Using ActiveAdmin, in the Index action of the Post, I am displaying a filter of PostSource this way: 使用ActiveAdmin,在Post的Index操作中,我以这种方式显示PostSource的过滤器:

filter :post_source, label: 'Source'
filter :category, as: :select, collection: Category.order(:name).collect { |cat| [cat.name, cat.id] }

with the controller as: 与控制器一样:

controller do
  def scoped_collection
    end_of_association_chain.includes(:post_source)
  end
end

It displays the source, but does not display in a sorted order. 它显示源,但不按排序顺序显示。 How to sort the filter in this case? 在这种情况下如何对过滤器进行排序?

I tried adding sortable, order on filter but it does not seem to work 我尝试在过滤器上添加可排序的顺序,但它似乎不起作用

If you're trying to sort the first filter ('post_source'), you could just add a collection as you have in your second line, and sort within a block. 如果您正在尝试对第一个过滤器('post_source')进行排序,您可以像在第二行中一样添加一个集合,然后在一个块中进行排序。

filter :post_source, label: 'Source', collection: proc { PostSource.order(:name) }

This syntax might be used as an alternative in the second example as well. 此语法也可以在第二个示例中用作替代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM