Forum: Ruby on Rails 'depot' app, where's session?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3e4f0299cf9270761530760543e03cdc?d=identicon&s=25 Audun Tønnesen (audun)
on 2006-04-16 20:41
Many of you probably know the 'depot' app from the 'Agile Rails
development' book.
I have constructed the 'products' model, the Store-controller, and the
'Cart' and 'Line Item' classes. I have told Application-controller about
: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
    @cart.add_product(product)
 #1#   breakpoint "hallo"
    redirect_to(:action => 'display_cart')
  end

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

  private
  def find_cart
#2#	session[:cart] ||= Cart.new
  end

#1#  At this breakpoint I can verify that the cart contains the correct
product. There's a difficulty with inspection, however: when I run the
breakpointer, the DOS-window seems to change characteristic, so that I
can't find the '@' on the keyboard, not even 'escape' it with 'alt-64'.
So, instead of inspecting @items directly, I instead have to inspect
'find_cart.items,' but that seems to work fine. However, this stops me
from breakpointing the 'display_cart' method, just because of the '@'
problem!!

The cart is already assigned to the session, see #2#.

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

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

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, of course. (Though, in the
cookie-list, I can't find any cookie which associates to this
application).

I'm totally stuck here. I've checked the code against the source one
million times. I'm beginning to distrust Rails (but from experience, I
know that most often, there's usually some stupid bug that's all my own
fault :-))

Any suggestions?

Regards, Audun.
3e4f0299cf9270761530760543e03cdc?d=identicon&s=25 Audun Tønnesen (audun)
on 2006-04-16 21:02
Audun Tønnesen wrote:
> Many of you probably know the 'depot' app from the 'Agile Rails
> development' book.

I have an addition to the main message: Of course, even tampered by the
keyboard '@' problem of the DOS-prompt within the breakpointer, I could
of course use the same indirect method: 'find_cart.inspect', AFTER the
change of view:

 def display_cart
      @cart = find_cart
      @items = @cart.items
    breakpoint "displayCartView"

  end

The result is as expected:   find_cart.inspect   evaluates to  [], the
contents of the cart is lost. In the 'add_to_cart' method, the
inspection showed the correctly selected product in the cart.

So it's most probably a session-bug.

Regards, Audun
A1c4a0bb61649f18a4d65b1c7273a50f?d=identicon&s=25 Christian Klauser (sealedsun)
on 2006-04-17 01:23
My depot application works perfectly on my Windows XP / Apache 2.0 /
FastCGI / Rails 1.1 configuration

The session cookie is named "_session_id" (without the ").
You could assign the Cart object to both @cart and a local variable cart
(two references to the same object) to inspect it.

What happens if you inspect the session content "session[:cart]". Do you
get nil?
3e4f0299cf9270761530760543e03cdc?d=identicon&s=25 Audun Tønnesen (audun)
on 2006-04-17 11:36
Christian Klauser wrote:
> My depot application works perfectly on my Windows XP / Apache 2.0 /
> FastCGI / Rails 1.1 configuration

Thanks for your help!
I run it with webrick, Rails 1.0.

Breakpoint in 'add_to_cart' ->

session[:cart] -> fine, the product/line_item is added to the
cart/session.

Breakpoint in 'display_cart' ->

session[:cart] contains an empty cart (items= [] and total_price =0.00)
(not nil)

This means that the original cart has been lost when the view shifts,
and there's a new session containing a new, empty cart, right?

In the browser, the only   _session_id  cookie comes from
'www.ruby_forum.com' from a few minutes ago),  not from my localhost.

Is it a cookie-problem, then?
A1c4a0bb61649f18a4d65b1c7273a50f?d=identicon&s=25 Christian Klauser (sealedsun)
on 2006-04-17 12:29
There is an easy way to check that.

Add <%=session.session_id%> somewhere in your `layout/store.rhtml`
When the session id changes every time, you refresh a store page,
something is wrong.

Additionally, you can check the HTTP Headers sent by the server (I used
the Firefox plugin 'Live HTTP Headers'
http://livehttpheaders.mozdev.org/):

Request:

        GET / HTTP/1.1
        Host: ruby-depot
        .../blah blah blah/...
        Cookie: _session_id=f8cc6e5b1a1d7bbd599f48e08dd56f90

Response:

        HTTP/1.x 200 OK
        Date: Mon, 17 Apr 2006 10:24:04 GMT
        Server: Apache/2.0.55 (Win32) PHP/5.1.2 mod_fastcgi/2.4.2
        Set-Cookie: _session_id=f8cc6e5b1a1d7bbd599f48e08dd56f90; path=/
        .../blah blah blah/...

 * Either the server does not send the session cookie in the response
 * Or your browser does not send the cookie back to the server in the
request
3e4f0299cf9270761530760543e03cdc?d=identicon&s=25 Audun Tønnesen (audun)
on 2006-04-17 18:56
First of all, I did some more breakpointing in the 'display_cart'
method:

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

At the second #2# breakpoint, there was an emty 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 doesn't send the session cookie to the
browser.  Do you agree?
3e4f0299cf9270761530760543e03cdc?d=identicon&s=25 Audun Tønnesen (audun)
on 2006-04-17 20:43
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.

I'm puzzled. Can I tweak WEBrick to do its job?

Regards, Audun
3e4f0299cf9270761530760543e03cdc?d=identicon&s=25 Audun Tønnesen (audun)
on 2006-04-17 21:03
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.

In the Store-view, the _session_id  is always the same, and in the 'Live
HTTP Headers' window, there is also a _session_id  which is the same at
every access to the page, BUT THE TWO are DIFFERENT.  Every time the
same, but ecery time different!

Do you understand anything?

regards, Audun.
5466d90bc9c5659a95697d704d7e0f0f?d=identicon&s=25 Martin (Guest)
on 2006-04-23 19:09
Hi just wanted to check, since I had the same problem as you. Could you
make sure the private store controller method find_cart actually is:

   session[:cart] ||= Cart.new
The equals above is very important as this proved to be my problem (tisk
tisk). If this is the case check the back of Agile Rails book and see
the source code for this page, the line has been amended to (try that):

  @cart = (session[:cart] ||= Cart.new)

I noticed that the add_to_cart method had actually got the @cart by
creating a temp page with this name and the <%= debug(@cart) %> which
showed all was fine there. (nb you have to remove the redirect part to
do this)

Hope that helps.

Regards, Martin.
248ce264f18adcf754340c279d5403aa?d=identicon&s=25 Mustapha Mustapha (mustapha)
on 2006-12-19 17:11
hi guys,
I got the same problem, I tried to understand what you have written in
order to resolve this issue in vain, what should I do exactly?

thanks and best regards,


Martin wrote:
> Hi just wanted to check, since I had the same problem as you. Could you
> make sure the private store controller method find_cart actually is:
>
>    session[:cart] ||= Cart.new
> The equals above is very important as this proved to be my problem (tisk
> tisk). If this is the case check the back of Agile Rails book and see
> the source code for this page, the line has been amended to (try that):
>
>   @cart = (session[:cart] ||= Cart.new)
>
> I noticed that the add_to_cart method had actually got the @cart by
> creating a temp page with this name and the <%= debug(@cart) %> which
> showed all was fine there. (nb you have to remove the redirect part to
> do this)
>
> Hope that helps.
>
> Regards, Martin.
This topic is locked and can not be replied to.