如何在 cookie rails 4 上存储数组? [英] How to store array on a cookie rails 4?
本文介绍了如何在 cookie rails 4 上存储数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 rails 上存储一个数组,但在解码时出现错误.我使用 cookies[:test] = Array.new
当我试图解码时@test = ActiveSupport::JSON.decode(cookies[:test])我收到一个错误.实现我想要实现的目标的正确方法是什么?
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 = ActiveSupport::JSON.decode(cookies[:test])
I am getting an error.
Whats the proper way to achieve what I am trying to ?
推荐答案
使用 session
,而不是 cookies
.您不必解码它,rails 会为您处理.以与您相同的方式创建会话:
Use session
, not cookies
. You don't have to decode it, rails handles that for you. Create the session the same way you already are:
session[:test] = Array.new
当你需要它时,像往常一样访问它
and when you need it, access it like normal
session[:test]
# => []
这篇关于如何在 cookie rails 4 上存储数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文