Topic: Update session cart

Using the depot from Agile Rails - I'm trying to update  the session[:cart] from inside or after the display_cart.rhtml.

I'm having a hard time getting at the session[:cart]

In the ./script/console, I can create a new Cart:


  - Cart.new         
=> #<Cart:0x2515678 @total_price=0.0, @items=[]>

But I can't seem to find the existing one... any clues??

TIA

Chas

Re: Update session cart

If I understand you correctly, you are creating a cart from a web browser which is getting saved to session[:cart], then you are trying to access it from script/console? This can't be done because the session key is stored in the browser's cookies. Rails checks the cookie and fetches the session information for that browser. If you access the site from anything else - another browser, or script/console - you won't get the same session information. Even if you quit/close the browser it will give you a new session because they are temporary.

Not sure if this answers your question though.

Railscasts - Free Ruby on Rails Screencasts

Re: Update session cart

makes sense, thanks. I am just looking for any way to "get" at that session in order to find out how to change it.

Basically, I want to assemble a list of items from a 'products' table and change prices/quantities after the fact.
Maybe it will be easier to just write em to the db and then change things, but i was hoping to learn how to access the session[:cart]  and change things on the fly - before saving to the db.

But, I'm groping here a bit, and just trying to "get it".

Re: Update session cart

The method in the AWDwR book of storing the entire Cart object in a session is a little strange, and I wouldn't recommend it. He points this out in the 2nd edition and says you should really store the cart in a database and just reference the "id" of the cart in the session. This is because, if you ever change the Cart class, then the changes will not take effect on the already instantiated Cart objects in the sessions. You will need to clear out the sessions or somehow update them to the updated Cart model. Ultimately though, it is much easier to just store it in the database. I have no clue why he teaches you the "wrong" way to do it, and it's one of my biggest peeves about the book.

Regarding your specific problem, there's probably some way to read a given session's content in Rails, but I don't know how off the top of my head.

Last edited by ryanb (2006-08-15 19:52:07)

Railscasts - Free Ruby on Rails Screencasts

Re: Update session cart

thanks....that makes so much more sense...