Dave says:
Just add something like
You've been here <%= pluralize(@count.to_i, "time") %>
to your view.
Bill says,
So here's a dumb question… If @count is a Fixnum, why use the to_i method which apparently simply returns @count?
Jim says,
The following seems to work (although there may be a cleaner way… and I'm not completely sure if I need the <% end %> part):
You have accessed this page <%= @count %> <% if @count == 1 %> time. <% else %> times. <% end %>
James says,
I just did:
<%= pluralize(session[:counter], "page load") %>
john joyce says:
What I did was:
You've accessed this page <%= pluralize(@count., 'time') %>
I don't see the point in .to_i either. The code for the counter already makes it a Fixnum anyway. Is there a potential security risk?
pfig says
yeah, i also just used
<%= pluralize( @count, 'time' ) %>
anything we should know?
PojoGuy says:
Both
<%= pluralize( @count, 'time' ) %>
and
<%= pluralize( @count, 'times' ) %>
work!
Dave says:
I didn't like sticking logic in the templates. So I did (and failed) the following, in views/store/index.html.erb
<%= count_index_visits -%> <h1>Your Grapmatic catalogue</h1> <p><%= report_index_visits -%></p>
Then, in (helpers/store_helpers.rb), added
def reset_index_visits session[:counter]=0 end def count_index_visits if session[:counter].nil? reset_index_visits else session[:counter]+=1 end end def report_index_visits if session[:counter]==0 then reponse="Welcome!" else response="Back for your "+session[:counter].to_s+"th visit?" end return response end
how to I stop count_index_visits dumping a number inside the layout?
more pressingly, how do I make reset_index_visits available to the whole application? it seems to fail when I move it to controllers/application.rb, and DRY says it should only appear in one place…?