仅显示 act_as_taggable_on 标签云中最流行的标签 [英] Showing only the most popular tags in the acts_as_taggable_on tag cloud

查看:42
本文介绍了仅显示 act_as_taggable_on 标签云中最流行的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Acts-as-taggable 效果很好,一切都很好,但我想知道是否有办法将标签云限制为仅使用最流行的标签?是的,它看起来不像是按照创建标签的顺序对我的云进行排序.

Acts-as-taggable works great and everything but I was wondering if there was a way to restrict the tag cloud to only the most popular tags? Right not it looks like its ordering my cloud by the order in which the tags were created.

但让标签云只显示最流行的标签更有意义.

But it makes more sense to have the tag cloud show only the most popular tags.

我的控制器有:

  def styles_tag_cloud
     @tags = Tattoo.tag_counts_on(:styles).limit(40)
  end

我的观点是:

<% tag_cloud(styles_tag_cloud, %w(css1 css2 css3 css4)) do |tag, css_class| %>
  <%= link_to tag.name, { :action => :tagged, :controller =>:index, :id => tag.name }, :class => css_class %>
<% end %>

但这只是显示创建的前 40 个标签,然后根据每个标签的使用次数调整其大小

But all this does is display the first 40 tags created, and then sizes each tag according to how many times its used

推荐答案

您可以使用 MyModel.tag_counts_on(:tags) 获取标签列表,按标签计数降序排列(最常用的标签首先),如果您想将其限制为特定数字,您可以将 .limit(my_magic_number) 附加到它.

You can use MyModel.tag_counts_on(:tags) to get a list of tags, ordered by tag count descending (most used tag first) and if you want to limit that to a specific number, you can just append .limit(my_magic_number) to it.

因此,要获得 Post 模型中最流行的 10 个标签的列表,您可以执行以下操作:

So to get a list of the 10 most popular tags on your Post model you'd do something like this:

@tag_counts = Post.tag_counts_on(:tags).limit(10)

如果您想查看每个标签被使用了多少次,@tags 中的每个对象都有一个 count 属性,您可以查看.

If you then want to see how many times each tag has been used, the objects in @tags each have a count attribute you can look at.

(摘自我下面的一个评论)......如果你想以特定顺序(最常用的标签在前)和一些外部定义的限制标签,你可以使用这个:Post.tag_counts_on(:tags).order('count desc').limit(however_many_you_want)

(extracted from one of my comments below) ...and if you want to the tags in a specific order (the most used tags first) with some externally defined limit, you can use this: Post.tag_counts_on(:tags).order('count desc').limit(however_many_you_want)

这篇关于仅显示 act_as_taggable_on 标签云中最流行的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆