Powered by SmartDoc

Controlling cookie objects

Creating cookies

Give arguments of initialize() a name or a pair of name/value. The value of cookie is omittable.

cookie = CKCookie.new( name, value )

Getting cookies from a request object

CKRequest has some methods for getting cookies. The methods are cookie(key), cookies, cookie_value(key), cookie_values(key). You can get CKRequest objects by CKApplication#request.

Getting cookies methods of CKRequest
Method Description
cookie(key) Returns a CKCookie object whose keyis the same as the argument.
cookies Returns an array of CKCookie objects.
cookie_value(key) Returns the value of CKCookie object whose keyis the the same as the argument.
cookie_values(key) If the argument is nil(by default, argument is nil.), this method returns an array which has all the values of cookies. Otherwise, it returns an array which has the values of cookies specified by the argument.

Setting cookies to a response object

CKResponse has methods for setting cookies. These methods are defined in CKMessage, the superclass of CKResponse. Use add_cookie(cookie) and remove_cookie(cookie).

cookie = CKCookie( 'name' )
application.response.add_cookie( cookie )

Removing cookies from browser

Send cookies with the same name to browser. If you set past expiration time for the cookie when of that, browser removes the cookie completely.

cookie = CKCookie "name"
cookie.expire = Time.new - 60
response.add_cookie cookie

Removing cookies from a response object

CKResponse#remove_cookie removes cookies in CKResponse object.

application.response.remove_cookie( 'name' )