0

我想在每一页的布局顶部都有一个计数器。计数器仅显示查询的计数。

我怎样才能让这个方法在每个页面上运行?

/控制器/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%>。前者只是不显示任何内容,而后者则显示错误。

4

1 回答 1

2

before_filter您需要使用 a 调用该方法ApplicationController

class ApplicationController < ActionController::Base
  before_filter :unviewed_count

  ....
end
于 2013-02-06T22:15:38.930 回答