On 17 January 2012 16:24, Agis A. <[email protected]> wrote:
> 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:
>
>> def increment_counter
>>     session[:counter] ||= 0
>>     session[:counter] += 1
>>     @times_visited = session[:counter]
>>   end
>>
>>   def reset_counter
>>     session[:counter] = 0 unless session[:counter].nil?
>>   end
>>
>>   def display_counter
>>     if @times_visited > 5
>>       @counter = "You have visited this page " + pluralize(@times_visited,
>> " times")
>>     end
>>   end
>
>
> 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?

Where are you calling display_counter from?  I would have expected it
to be from the view, in which case put the method display_counter in a
view helper not in the controller.  Pass it the value @times_visited.
So in the view:
<%= display_counter @times_visited %>

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to