views:

69

answers:

2

I have a Rails app that is storing some user information in a cookie, and that information isn't being stored as soon as I would like. A client that is performing five requests in a row from a single page has an empty cookie until the page has completely loaded, and then for the future, the cookie is populated. It is behaving as if the client doesn't actually commit the cookies until the page is loaded. This is true using multiple browsers, so it doesn't appear to be purely a quirk of any one particular browser. Is there any way to commit those, or am I going to have to find another workaround?

A: 

The cookie is set on the visitor's browser only after the response is returned, so you won't find the value in the cookie during the request.

If you set cookies[:some_key] to a value when you're processing the request on a Rails controller, can't you just look up the value you just set later on? How exactly are you setting and looking up the cookie value?

dan
The client page is making five calls to the server, and I'm setting cookies[:value] in the controller of the first call. However, when the second call comes along, cookies[:value] is nil, so I re-create the cookie. After the page finishes loading, if I refresh the client page, the cookie is set to the expected value.
jjb
A: 

Could you explain in more detail what you're doing to generate five rails requests in the context of a single page load? Can you guarantee the order in which these requests will arrive at the web server?

I'm guessing that cookies aren't going to be the optimal way of accomplishing what you're trying to do.

Cratchitimo
Basically, each image that is tagged with a particular style generates a request to the server to return some additional information about the image. As part of that transaction, the server is also returning a visitor ID that is stored in a cookie so that we have a way of recording that a request from a particular visitor for statistical purposes.
jjb
So I'm guessing that the user isn't logged in or anything before this transaction starts? Would you be able to append some unique key to the query string of each of these URLs that would tie them together as a transaction?
Cratchitimo
No, the user isn't logged in, and those users won't have accounts with us. We've considered creating a unique page ID that is sent with each call to the server in order to track pages (versus individual calls for each image).
jjb
Yeah, my thinking is that will be a better bet than dealing with the idiosyncrasies of cookie handling in this case. :/
Cratchitimo