问题
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