Forum: Ruby on Rails 'depot'app, trouble with session / cookies

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Audun T. (Guest)
on 2006-04-18 20:10
Many of you probably know the 'depot' app from the 'Agile Rails
development' book.

When the view changes from 'Store' to 'display_cart,'  a session
containing the shopping-cart (:cart) is supposed maintain the 'cart'
between calls, but when the display_cart stub view shows up, I have
always ZERO elements in the cart!

I have received valuable help already, but the issue seems  less
understandable than I thought at first. Here's a resumen:

I have constructed the 'products' model, the Store-controller, and the
'Cart' and 'Line Item' classes. I have told Application-controller
:cart and :line_item:

model  :cart
model  :line_item

Here's part of the Store_controller:

def add_to_cart
    product = Product.find(params[:id])
    @cart = find_cart
 #1#   breakpoint "hallo"
    redirect_to(:action => 'display_cart')

  def display_cart
      @cart = find_cart
      @items = @cart.items

  def find_cart
	session[:cart] ||=

#1#  At this breakpoint I can verify that the cart contains the correct
product, and the session is ok.

Then the view changes to the test-stub 'display_cart,' which looks like

<h1>Display Cart</h1>
Your cart contains <%= @items.size %> items.

Every time, no matter how many times I back up and add products, the
@items.size evaluates to ZERO. So the session and the cart get lost.
Both my browsers have cookies enabled, but in the
cookie-list, I can't find any cookie which associates to this
application (under WEBrick).

Any suggestions?

(New msg:)
I did some more breakpointing in the 'display_cart'

def display_cart
     # 1 # breakpoint "hello"
      @cart = find_cart
      @items = @cart.items
    # 2 # breakpoint "hello"

At the second #2# breakpoint, there was an empty session, but that was
of course created by the 'find_cart' call. At the first #1# breakpoint,
the session was in fact nil.

When I set  <%=session.session_id%>  in the views, it is always exactly
the same in the 'store' view, (but changes every time in the
'display_cart' view).
And the _session_id COOKIE never appears in Firefox cookie-list.
From this, it seems the server (WEBrick) doesn't send the session
cookie to the browser.

(New msg:)

Yeah, now I use 'Live HTTP Headers,' and it shows that WEBrick never
sends the cookie to Firefox from my application.
All other web-sites send cookies to their heart's delight, I can see
that directly in the 'Headers' window.

(New msg:)

This is getting stranger all the time.....
I've switched to Apache (v. 1.33....). It's  s-l-o-w, extremely slow,
but the pages keep popping up correctly.

But the cart still always contains ZERO items!

In the Store-view,(where I peek with <%=session.session_id%>), the
_session_id  is always the same, and in the 'Live HTTP Headers' window,
there is also a _session_id from 'add_to_cart' which is the same at
every access to the page, BUT THE TWO are DIFFERENT.  Rephrasing it more

Every time I go back to the 'Store-view," I can see that the session-id
is the same at every page access (peeking with <%=session.session_id%>),
but in the browser's 'Live HTTP Headers' window, the _session-id is a
different one, but always the same at recurring sendings to the browser.

The session-id ought to be the same, doesn't it?

Do you understand anything? I've been stuck here for nearly 2 weeks
(Thanks to Christian K. for great help in circling in the bug)

Reagards, Audun.
This topic is locked and can not be replied to.