Cookies
Actions can set cookies on outgoing requests via the response
object.
.cookies["tasty_cookie"] = "strawberry"
end
end
end
end
end
response
Cookies subsequently sent by the browser can be read from the request
.
.cookies["tasty_cookie"] # => "strawberry"
end
end
end
end
end
request
Cookie configuration
You can set one or more of the following options for cookies issued by actions using the action cookies config on your app.
:domain
-String
(nil
by default), the domain:path
-String
(nil
by default), a relative URL:max_age
-Integer
(nil
by default), cookie duration expressed in seconds:secure
-Boolean
(true
by default if using SSL), restrict cookies to secure connections:httponly
-Boolean
(true
by default), restrict JavaScript access to cookies
# config/app.rb
config.actions.cookies = {
domain: "hanami.example.com",
secure: true,
httponly: true,
path: "/foo",
max_age: 300
}
end
end
This configuration can be overridden within an action passing a hash, which has a value
key representing the value of the cookie, and any properties to override.
.cookies["tasty_cookie"] = "strawberry"
response.cookies["longer_lived_cookie"] = {
value: "anzac_biscuit",
max_age: 604800
}
end
end
end
end
end
response
Removing cookies
To remove a cookie, assign it the value nil
.
response.cookies["tasty_cookie"] = nil
Disabling cookies
To prevent cookies from being set in your actions, provide the following config to your app:
# config/app.rb
config.actions.cookies = nil
end
end