# Why isnt my simple counter working?

**URL:** <https://www.ruby-forum.com/t/why-isnt-my-simple-counter-working/84830>\
**Category:** Rails\
**Created:** [February 8, 2007, 12:27am UTC](https://www.ruby-forum.com/t/why-isnt-my-simple-counter-working/84830 "2007-02-08T00:27:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kristen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c4cdca/32.png) [@Kristen](https://www.ruby-forum.com/u/Kristen)\
**Post date:** [February 8, 2007, 12:27am UTC](https://www.ruby-forum.com/t/why-isnt-my-simple-counter-working/84830/1 "2007-02-08T00:27:48Z")

</div>

Hello,

Im on chapter 8 of the agile book and I cant get this simple counter to  
work.  
Its suppose to count how many times a person visited the home page and  
display it when its more than 5.

store\_controller.rb:

def index  
@count = increment\_count  
end

def increment\_count  
if session[:counter].nil?  
session[:counter] = 0  
end  
session[:counter] += 1  
end

store.rhtml:  
\<% if @count \> 5 %\>  
\<%= @count %\> visits  
\<% end %\>

This works when I just refresh the page on the home page, but when I  
click on the “add to cart” button which envokes the add\_to\_cart method  
and directs to the add\_to\_cart view, then it gives me an error that  
@count is nil.

NoMethodError in Store#add\_to\_cart

Showing app/views/layouts/store.rhtml where line #21 raised:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.\>

Why does @count suddenly become nil when it has value before?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Kristen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c4cdca/32.png) [@Kristen](https://www.ruby-forum.com/u/Kristen)\
**Post date:** [February 8, 2007, 12:43am UTC](https://www.ruby-forum.com/t/why-isnt-my-simple-counter-working/84830/2 "2007-02-08T00:43:03Z")

</div>

On 2/8/07, Kristen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> NoMethodError in Store#add\_to\_cart
> 
> Showing app/views/layouts/store.rhtml where line #21 raised:
> 
> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.\>
> 
> Why does @count suddenly become nil when it has value before?
> 
> Thanks in advance!

You’ve set the @count variable in the index method. Is it set in your  
add\_to\_cart method as well? Perhaps you could extract that into a  
before\_filter to set @count for you. Something like this may get you  
started

before\_fitler :set\_visits

def index  
increment\_count  
set\_visits  
end

private

def increment\_count  
session[:counter] ||= 0  
session[:counter] += 1  
end

def set\_visits  
@count = session[:counter]  
end

---

<div class="post-metadata">

**Author:** ![Kristen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c4cdca/32.png) [@Kristen](https://www.ruby-forum.com/u/Kristen)\
**Post date:** [February 8, 2007, 4:27am UTC](https://www.ruby-forum.com/t/why-isnt-my-simple-counter-working/84830/3 "2007-02-08T04:27:48Z")

</div>

> before\_fitler :set\_visits
> 
> def index  
> increment\_count  
> set\_visits  
> end
> 
> private
> 
> def increment\_count  
> session[:counter] ||= 0  
> session[:counter] += 1  
> end
> 
> def set\_visits  
> @count = session[:counter]  
> end

thanks for the tip. Ill have to take a look at the filters since i dont  
knwo anything about them yet.
