Can store hash in a cookie?

前端 未结 3 2115
执笔经年
执笔经年 2021-02-20 10:38

Anyone know if I can put a hash in the cookie? Something like this: cookies [: test] = {: top => 5,: middle => 3,: bottom => 1}

Thanks

3条回答
  •  耶瑟儿~
    2021-02-20 10:49

    There are a number of ways which it is possible (i.e. storing a string and evaling that value, SCARY!). This is a simple way.

    cookies[:test_top]    = 5
    cookies[:test_middle] = 3
    cookies[:test_bottom] = 1
    

    You can also convert to JSON and then parse it when loading the cookie.

    Newer versions of Rails include automatically serialization using the session object.

提交回复
热议问题