DEV Community

Samuel Lubliner
Samuel Lubliner

Posted on

The Cookies Hash

Cookies: server stores a set of key-value pairs in the client

Server must include the Set-Cookie header in a response

add gem "sinatra-contrib" to our Gemfile, bundle install, and within app.rb:

require "sinatra/cookies"

Within an action, we can use the cookies hash with Hash methods like .store, .keys, .fetch, [ ], etc.

Cookies are used for many things: analytics, ad targeting, etc, signing in a user, etc.

Only store strings in cookies

Store an Array or Hash of values within a single cookie. Convert the Ruby Array or Hash into a JSON string, and then store the string

JSON.generate() accepts an Array or a Hash as an argument and returns a String

Top comments (0)