Just add something like
You've been here <%= pluralize(@count.to_i, "time") %>
to your view.
What do you guys think about using the flash[:notice] space to display this count? This way, additional logic does not need to be dropped into the view. I am not able to get the pluralize helper to work inside the flash though… Any comments?
def index @products = Product.find_products_for_sale @count = session_count @session_greeting_msg = session_greeting flash[:notice] = @session_greeting_msg end def session_count if session[:counter].nil? session[:counter] = 0 else session[:counter] += 1 end end def session_greeting if @count == 0 session_greeting = "welcome!" else session_greeting = @count end end
So here’s a dumb question… If @count is a Fixnum, why use the to_i method which apparently simply returns @count?
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 %>
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 saysyeah, i also just used
<%= pluralize( @count, 'time' ) %>
anything we should know?
<%= pluralize( @count, 'time' ) %>
<%= pluralize( @count, 'times' ) %>
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…?
Trientalis says to Dave:
Perhaps you could try to write the helper method in the application_helper instead – then it should be available to the whole application.
Has anyone tried to put the counter in the layout? To do this I tried to set the @count variable in the “initialize” method, but there seems to be some constraints with using sessions in that method. Is there a workaround?
I did try using it in the layout, and it worked. What I did was placed the method in private and called it ou from there in the layout. Works same way as in index.
I added in my StoreController in index method:
session[:counter] ||= 1 session[:counter] += 1
and somewhere in application layout:
<%= session[:counter] %>
- V18: Mark Young [almost 5 years ago]
- V17: Paweł Damasiewicz [almost 6 years ago]
- V16: Paweł Damasiewicz [almost 6 years ago]
- V15: Andrew de Andrade [about 7 years ago]
- V14: Andrew de Andrade [about 7 years ago]
- V13: Wesley Tarle [over 7 years ago]
- V12: Wesley Tarle [over 7 years ago]
- V11: Alexey Efremov [almost 8 years ago]
- V10: Alexey Efremov [almost 8 years ago]
- V9: Alexey Efremov [almost 8 years ago]