How to save multiple values ​​inside a single cookie in Ruby on Rails?

I am learning how to work with cookies in Ruby on Rails. All I know is to set the name and value of the cookie, but I want to save up to three fields. Therefore, any tips or good lessons will be appreciated!

Thanks for any help.

+3
source share
3 answers

Cookies, by definition, consist of a single name / value pair, where both fields are textual. You must really use three separate cookies to store individual values.

cookies["value_1"] = "one"
cookies["value_2"] = "two"
cookies["value_3"] = "three"

- cookie, , ( , ~~ ):

value_1 = "one"
value_2 = "two"
value_3 = "three"
cookies["multiple_values"] = "#{value_1}~~#{value_2}~~#{value_3}"

cookie, , cookie, . , .

+5

cookie . (, - UUID), , .

+1

You can assign a cookie array in rails with

cookies[:my_array] = [12, 1234]

and read the array

cookies[:my_array]   # => [12, 1234]

http://api.rubyonrails.org/classes/ActionDispatch/Cookies.html

0
source

Source: https://habr.com/ru/post/1796047/


All Articles