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%>
。前者不顯示任何內容,而後者顯示錯誤。
你使用任何'before_filter'在'ApplicationController'調用函數? – iltempo
不,我不是。我忘了我必須這樣做。由於我更新到rails,所以不習慣在ApplicationController中工作。 – Catfish