views:

58

answers:

1

I have set up an asset host at assets.domain.com but it appears that cookies are being sent with requests to assets.domain.com. I read somewhere that if you have cookies set to domain.com then this will happen.

So I guess I'm trying to set cookies only to www.domain.com so that requests to assets.domain.com will not send cookies. (I also have a permanent redirect from domain.com to www.domain.com)

How do I do this in Rails??

A: 

To set a cookie on specific domain:

cookies[:my_cookie] = {:value => 'Tasty Cookie', :domain => 'www.domain.com'}

One gotcha is that you must also specify the domain when you delete domain-specific cookies:

cookies.delete(:my_cookie, :domain => 'www.domain.com')

To make sure I don't forget, I usually make a helper for setting and deleting cookies where the default domain is always specified.

jdeseno
Hi, is there some sort of global setting so all my cookies are set on that subdomain??
Steven Ou
There are for session cookies (google for it) but, regular cookies would still be on the old domain. If you don't like the idea of setting/deleting cookies from a helper method, I've seen people get around this by extending ActionController::Cookies::CookieJar to include their default domain.
jdeseno