Hello there ppl.
I want to have a counter which will:
- count the number of times that the ‘index’ controller action is called and store this in a cookie
- will be displayed in the view only if the times visited is greater than 5
- it must look like this: “You have visited this page 1 time” or “You have visited this page 2 times” (I know I can use the pluralize() method for that)
We always say that the views must know as little as possible regarding the logic & data of our apps, so I guess the ideal way is to just render it through a <%= @counter %> piece of code. Right until here? I proceed,
in my ApplicationController I’ve added these methods regarding the counter:
session[:counter] ||= 0
session[:counter] += 1
@times_visited = session[:counter]
session[:counter] = 0 unless session[:counter].nil?
if @times_visited > 5
@counter = “You have visited this page " + pluralize(@times_visited, " times”)
The last method is what I’m interested in right now. It gives me a undefined method `pluralize’ for #StoreController:0x007fdbab23c9c0 error cause I’m trying to access a helper from a controller and not from a view. Correct me if I’m wrong.
So I could hard-code the IF statement into the view but this would break the rules of MVC architecture, right? So what would be the best way to do this?