2014-07-11 61 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