Rails acts_as_taggable_on grouped alphabetically?
Having sorted th开发者_Python百科e tag_counts hash via the following code:
sorted_tags = Contact.tag_counts.sort{ |x,y| x.name.downcase <=> y.name.downcase }
what is the easiest/most efficient way to display the tags in my view grouped by letters?
i.e
A - "Alpha", "Apple", "Aza"
B - "Beta", "Bonkers"
.
.
.
Z - "Zeta", "Zimmer"
Any ideas?
Ok I found a way, not sure if its the most efficient or most elegant but here goes:
-sorted_tags = Contact.tag_counts.sort{ |x,y| x.name.upcase <=> y.name.upcase }.map(&:name)
%ul
-"A".upto("Z") do |l|
%li="#{l} = #{ sorted_tags.select{ |x| x.upcase.starts_with?(l)}.map{|k| link_to k, k}.join(" ") }"
精彩评论