Catfish
Catfish

Reputation: 19284

Rails how to show a count in my layout from a query that is in my applicationController

I want to have a counter in the top of my layout that on every page. The counter just shows the count from a query.

How can i get this method to be run on every page?

/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 %>

I tried <%= @count %> and <%= unviewed_count%>. The former just doesn't show anything, while the latter shows an error.

Upvotes: 0

Views: 189

Answers (1)

Shane Andrade
Shane Andrade

Reputation: 2675

You need to call the method using a before_filter in your ApplicationController:

class ApplicationController < ActionController::Base
  before_filter :unviewed_count

  ....
end

Upvotes: 2

Related Questions