How to store array on a cookie rails 4?

前端 未结 3 1077
再見小時候
再見小時候 2021-01-20 01:03

I am trying to store an array on rails an getting error on the decoding. I use cookies[:test] = Array.new And when I am trying to decode @test = ActiveSupp

3条回答
  •  天命终不由人
    2021-01-20 01:46

    The "Rails way" is to use JSON.generate(array), since it's what is used in the second example in the Cookies docs:

    # Cookie values are String based. Other data types need to be serialized.
    cookies[:lat_lon] = JSON.generate([47.68, -122.37])
    

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

    When you want to read it back, just use JSON.parse cookies[:lat_lon] for example, and it'll provide you an array.

提交回复
热议问题