Pt-D-5
Hello i used this code :
class StoreController < ApplicationController
def increment_counter
if session[:counter].nil?
session[:counter] = 0
end
session[:counter] += 1
end
def index
@count = increment_counter
@products = Product.all
@time = Time.now
@shown_message = "You've been here #{@count} times" if increment_counter >5
end
end
and in the index.html.erb
<%= @shown_message %>
but it does not give the correct number. The sessions are increasing by two . like 5 times , 7 times , 9 times . can you help me?
Justin says:
You’re calling “increment_counter” twice in one function. That will increment the number two times.
Try this:
@shown_message = “You’ve been here #{@count} times” if session[:counter] >5
Bess says:
I used this code in the application_controller (the check if greater than 5 was my only new addition):
def increment_counter
if session[:counter].nil?
session[:counter] = 0
end
session[:counter] += 1
if session[:counter] > 5
return session[:counter]
end
end
Then in the store view I checked to make sure of the existance of @counter before display.
<% if @counter %> <p><%= pluralize(@counter, "page load") %></p> <% end %>
Is this the right way to go, or am I still complicating the view too much with the additional if?
Additional question: Is there a way to write functional unit tests for the counter? Are session variable available? Seems to not be the case.. you can either test against Objects themselves or selectors…?

