how to add sorted elements in the filter dropdown in activeadmin rails

家住魔仙堡 提交于 2019-12-23 09:17:39

问题


I have a Post model, PostSource model. A PostSource has many posts, a post belong to one PostSource.

Using ActiveAdmin, in the Index action of the Post, I am displaying a filter of PostSource this way:

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


回答1:


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.

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

This syntax might be used as an alternative in the second example as well.



来源:https://stackoverflow.com/questions/28451274/how-to-add-sorted-elements-in-the-filter-dropdown-in-activeadmin-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!