Sorry - this is actually about Rails - and not those kind of cookies (Thought if you need a recipe you can try Ubuntu Cookies from Make)
I’ve been working on integration with another web app all week. This ended up meaning that I spent a lot of time looking at cookies.
Since most of the time the app minds its own business, I’m focused on flash and session. So I realized pretty quickly that I haven’t actually manually set a cookie in Rails before.
It turns out that it is pretty easy but a little confusing. Cookies follow the standard Rails pattern of session,flash etc.
There is just one thing that might trip you up. Basically you have to think of cookies as a Hash with a worm whole in it. (I didn’t make that analogy but I like it).
Basically if you read from cookies you get was was submitted as part of the request. If you write to cookies you will set a cookie in the browser. The confusing part appears to be that if you say
cookies[:timezone] = “CST”
If you put in
cookies[:timezone]
It will be nil. This is because you didn’t get that cookie on the way in. Make sense? Yeah I think the same thing. I figure it exists that way because most people don’t end up messing with them very much.
So in my case I actually needed to set a more complicated cookie. It needed to be accessible to all machines in a given domain and it needed to expire in 12 hours.
It turns out it is pretty easy to do both
cookies["TIMEZONE"] = {:value => “CST”, :expires => 12.hours.from_now, :domain => “.economysizegeek.com”}
Pretty simple - once you know what the options are. Of couse as soon as you start adding stuff like this you end up needing to be able to test cookies. In my case, the mock authentication system had some problems as a result.
Fortunately, Herryanto Siatono came to the rescue with Functional Test with Cookie
So now not only are cookies easier to work with in the app - they are easy to setup for tests as well.
Leave a Reply
Moderation Active: Old stuff here... Therefore your comment on this post will be moderated (i.e. don't submit twice !)