small medium large xlarge

21 Nov 2009, 10:24
Wheeler Chu (2 posts)

I added the increment_count method and invoked it in the index action:

class StoreController < ApplicationController
  def index
    @products = Product.find_products_for_sale
    increment_count # NOT work
    #@counter = increment_count  # it WORKs
    puts "-------------"
    puts session[:counter]
    puts "-------------"

# stuff..


  def increment_count
    if session[:counter].nil?
      session[:counter] = 1
      session[:counter] += 1

# stuff..


and try to display it in the view:

<div class="index-visited">You've visited this catalog <%= @counter %> time(s)</div>

then I got

You've visited this catalog  time(s)

no counter, but 2 blanks..

though the puts method always puts the right counts to the console, and no error message printed in the console or log, I couldn’t find out why increment_count just does not work

this is my environment:

localhost:depot-1 wheeler$ rails -v
Rails 2.2.2
localhost:depot-1 wheeler$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
localhost:depot-1 wheeler$ uname -a
Darwin WheelermatoMacBook.local 10.2.0 Darwin Kernel Version 10.2.0: Tue Nov  3 10:37:10 PST 2009; root:xnu-1486.2.11~1/RELEASE_I386 i386
21 Nov 2009, 10:25
Wheeler Chu (2 posts)

Got, I should write

<%= session[:counter] %>
You must be logged in to comment