Dave says:

Just add something like


You've been here <%= pluralize(@count.to_i, "time") %>

to your view.

robbyt says:

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

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…?

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.

Gonzalo says:

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?

Paulette says:

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.

Mark says

Hi Dave.
RE: “how to I stop count_index_visits dumping a number inside the layout?”

My understanding is that when you use ”<=” to insert ruby code into your HTML then any output it generates is placed in the web page, whereas if you type ”<” it does not. So in your case change the ”<= count_index_visits” to ”< count_index_visits”

You change ”<%= count_index_visits”