2014-07-11 38 views
1

我使用acts_as_taggable gem来获得我的引脚的分类(标签),但我无法生成适当的云标签,以获取所有标签的列表(实际上我只有2个型动物标签)Rails:标签云与acts_as_taggable

在我的应用程序/控制器/ pins_controller.rb

def tagged 
    if params[:tag].present? 
    @pins = Pin.tagged_with(params[:tag]) 
    else 
    @pins = Pin.postall 
    end 
end 


def tag_cloud 
    Pin.find(:first).pins.tag_counts_on(:tags) 
    @tags = Pin.tag_counts_on(:tags) 
end 

应用程序/模型/ pin.rb

acts_as_taggable_on :tags 
end 

应用程序/佣工/ pin_helpers.rb

module PinsHelper 
    include ActsAsTaggableOn::TagsHelper 
end 

而且在我的应用程序/视图/销/ index.html.erb

<% 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 %> 

但是当我试图进入电影我销指数我有:

NoMethodError in Pins#index 
undefined method `empty?' for nil:NilClass 

我在做什么错误?我跟着acts_as_taggable宝石的维基

编辑: 应用程序/控制器/ pins_controller.rb

def index 
    @pins = Pin.order(votes_count: :desc) 
    @pin_count = Pin.count 
    end 
+0

什么你pins_controller.rb中的索引操作看起来像? – nbon

+0

您也可以从'tag_cloud'方法中删除'Pin.find(:first).pins.tag_counts_on(:tags)',因为它没有被存储/返回到任何地方。 – tirdadc

+0

我用我的pin_controller.rb索引编辑了我的问题 – zacchj

回答

2

你需要真正拉你的代码会在index方法:

def index 
    @pins = Pin.order(votes_count: :desc) 
    @tags = Pin.tag_counts_on(:tags) 
    @pin_count = Pin.count 
end