small medium large xlarge


Simply reset the count in the session

def add_to_cart
  # ...
  session[:counter] = 0


I’m working with the 4th edition. I don’t see this add_to_cart method anywhere. What I did is add

session[:counter] = 0
to the create method in the line_items_controller.rb file.

UPDATE: Apparently the add_to_cart method was removed in version 4 of the book.


But what if the counter is not initialized? (like if someone pastes an add_to_cart url directly into the addressbar) Uh, then it is now initialized by the assignment.


Mark0 You could make a method for checking if the counter exists, initializing it in case it isn’t there.

If it’s there you could increment the count. On the add_to_cart method, then you can set it to 0 without trouble.


I did something like:

  session[:counter] = nil 

Why would the session be different than when the page was never loaded?


If the session is not initialized, won’t it be initialized by the assignment? yes


If you want to display @count in store.rhtml you have to write:

def add_to_cart
  # ...
  @count = session[:counter] = 0


I think the easiest way is the first one, it is no need to check whether the session is existed.

def add_to_cart
  # ...
  session[:counter] = 0


Oops! It’s strange that when I change the code

  @count = session[:counter] = 0

  @count = 0

It doesn’t display any count number on my cart page. Can anyone explain it to me?


I’m working with 3rd edition of the book (p7.0) and can’t find the add_to_card method as well. Instead I have add_product, but when I do:

@count = session[:counter] = 0

session[:counter] = 0
I get error:

NameError in StoreController#add_to_cart undefined local variable or method `session’ for # Cart:0xb6d9a894>

When I do:

@count = 0

the counter doesn’t reset. Any ideas?


When I finally put

session[:counter] = 0
in a correct file (store_controller.rb which is now obvious for me, lol) then it started to work.

On the other hand, I was looking for add_to_cart method in the index of the book and it’s not there. Why? This made me thinking it’s not in there at all!

I’m working on it too long and it’s definitely time take a break if I do such mistakes.

Ramon M.

I’m working with 4th Ed. What I did is a method in application_controller.rb:

  def reset_session_counter
    if not session[:counter].nil?
      session[:counter] = 0

And then a call from the line_items_controller.rb create’s method:

@line_item = => product)
    respond_to do |format|
        format.html { redirect_to(@line_item.cart, :notice => 'Line item was successfully created.') }

What do you think about it? I guess that’s a good solution, but I would like to kow from some of you.

John B.

I just added the add_to_cart method to the line_items controller and put @count = add_to_cart in the create method. That works fine for me. Any thoughts on possible problems with my solution?

Page History
  • V25: Leopoldo Garcia [about 5 years ago]
  • V24: Leopoldo Garcia [about 5 years ago]
  • V23: Leopoldo Garcia [about 5 years ago]
  • V21: Leopoldo Garcia [about 5 years ago]
  • V22: System [about 5 years ago]
  • V20: Leopoldo Garcia [about 5 years ago]
  • V19: Joseph Venator [about 6 years ago]
  • V18: Al Dente [over 6 years ago]
  • V17: Al Dente [over 6 years ago]
  • V16: John Benson [about 7 years ago]