acts_as_taggble标签云未定义方法`count'用于标记对象

时间:2012-08-24 01:59:29

标签: ruby-on-rails tag-cloud

我正在尝试使用acts_as_taggable实现标签云。我已经有了自己的标记模型,我希望能够重新使用当前的代码,并且只需要使用acts_as_taggle来实现云。所以在我的控制台中我可以做...

1.9.3p194 :005 > t=Tag.first
Tag Load (1.4ms)  SELECT "tags".* FROM "tags" LIMIT 1
=> #<Tag id: 1, path: "corporation", friendly_name: "Corporation", popular: false,  
hot_topic: false> 
1.9.3p194 :006 > t.items.count

(11.6ms)SELECT DISTINCT COUNT(DISTINCT“items”。“id”)FROM“items”INNER JOIN
   “tag_items”ON“items”。“id”=“tag_items”。“item_id”WHERE“tag_items”。“tag_id”= 1    =&GT; 534

我添加了acts_as_taggable gem,我有一个看起来像......的标签帮助器。

module TagsHelper
  include ActsAsTaggableOn::TagsHelper
end

在我的控制器里,我有......

class TagsController < ApplicationController
...

  def tag_cloud
    @tags = Tag.tag_counts_on(:tags)
  end
end

..在我看来,我有......

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

我从github上的gem readme中直接推出了这个。我得到的错误是......

undefined method `count' for #<Tag:0xbfe5438>

不完全确定它来自哪里。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

我最终创建了我的标签云,并修改了此处的代码...

http://www.idolhands.com/ruby-on-rails/guides-tips-and-tutorials/creating-a-heatmap-or-tag-cloud-in-rails

这是我的代码...... https://gist.github.com/3505320

希望这可以帮助别人!