2012-08-09 34 views
0

我目前在我的發佈模型上獲取了以下錯誤,該標記和通道位於act_as_taggable_on下。某些實例方法在acts_as_taggable中未定義

undefined local variable or method `tag_list_on' for # 
<ActiveRecord::Relation:0x007f9864675b48> 

我覺得rails無法檢測到tag_list_on或set_tag_list_on方法的存在;但是,它確實檢測了tagged_with方法,其源與其他文件位於完全相同的模塊中。

RubyMine可以檢測到所有這些方法的存在很好順便說一句。

以下是我正在執行所有這些操作的代碼部分。

@posts = Post.tagged_with(params[:tags]).paginate(:page => params[:page]|| 1, :per_page => 20) 
user_tags = @posts.tag_list_on(:tags)               
custom_tags = user_tags - params[:tags]              
@posts.set_tag_list_on(:customs, custom_tags)             
@tags = @posts.tag_counts_on(:customs, :order => "count desc").limit(10)      
@channels = @posts.tag_counts_on(:channels, :order => "count desc")       

回答

1

tagged_withPost一類方法中,由acts_as_taggable_on寶石加入。

@posts在您的代碼中是ActiveRecord::Relation的實例,而不是Post類本身或其任何實例。

ActiveRecord::Relation中沒有tag_list_on實例方法,因此是錯誤。

tagged_with說,它返回

...標記有指定標籤的對象的範圍。

tag_list_onset_tag_list_on實例Post類的方法,由所述寶石acts_as_taggable加入。

你需要調用tag_list_onset_tag_list_on@posts

user_tags = [] 
@posts.each do |post| 
    user_tags = user_tags + post.tag_list_on(:tags) 
end 
custom_tags = user_tags.uniq - params[:tags] 

# ... 
+0

真棒每個實例,但儘管告訴我。爲什麼他們只在ActiveRecord :: Relation中擴展使用某些方法,但並非所有主要方法都可能經常一起使用。 – jab 2012-08-09 19:43:39

+0

另外,你將如何去設置正確生成的標籤列表?如果我爲post的每個實例調用set_tag_list_on,然後調用@ posts.tag_counts_on(:customs,:order =>「count desc」)。我認爲我沒有得到正確的行爲。 – jab 2012-08-09 20:37:43

相關問題