2013-02-06 13 views
0

我想在每個頁面的佈局頂部有一個計數器。計數器只顯示來自查詢的計數。Rails如何從我的applicationController中的查詢中顯示佈局中的計數

我怎樣才能讓這個方法在每一頁上運行?

/controllers/ApplicationController.rb

def unviewed_count 
    p 'we in unviewed_count' 
    @count = Person.where("viewed = ?", '0').count 
    p @count 
end 

/views/layouts/layout.html.erb

<%= @count %> 

我試圖<%= @count %><%= unviewed_count%>。前者不顯示任何內容,而後者顯示錯誤。

+0

你使用任何'before_filter'在'ApplicationController'調用函數? – iltempo

+0

不,我不是。我忘了我必須這樣做。由於我更新到rails,所以不習慣在ApplicationController中工作。 – Catfish

回答

2

您需要調用使用該方法的before_filterApplicationController

class ApplicationController < ActionController::Base 
    before_filter :unviewed_count 

    .... 
end 
+0

不客氣! –

相關問題