如何在 cookie rails 4 上存储数组?

编程入门 行业动态 更新时间:2024-10-26 23:29:55
本文介绍了如何在 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 上存储数组?

本文发布于:2023-10-24 15:00:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何在   cookie   rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!