在Rails cookie中存储/检索值 [英] Storing/Retrieving values in a Rails cookie
问题描述
我创建了一个SessionHelper模块,其外观大致如下:
I created a SessionHelper module which looks more or less like this:
module SessionHelper
def create_cookie
cookies.signed[:token] = {
:value => [],
:expires => 1.hour.from_now
}
end
def cookie_values
cookies.signed[:token]
end
def add_value_to_cookie(value)
cookies[:token] << value
end
def cookie_exists?
cookies[:token].present?
end
end
然后将其包含到应用程序中控制器,并在每个请求之前在过滤器中触发:
It then gets included to the Application controller and gets fired in a filter, before every request:
create_cookie unless cookie_exists?
我的网站未使用身份验证,我想显示访客最近创建的项目。
因此,在Item控制器中,我有这样的东西:
My site does not use authentication, I would like to display the recent items that were created by a guest. So, in an Item controller I have something like this:
def create
@item=Item.new(params[:item])
if @item.save
add_value_to_cookie(@item.id)
redirect_to @item
else
render :new
end
end
问题如下:
- 之前的过滤器创建一个空的
cookie,我可以确认它的初始
值设置为[](确定) - 用户创建项目后,我希望cookie数组具有附加值,但相反它将变为Nil,并且没有任何内容(BAD)。它甚至不会引发异常。
我在哪里出错?我应该如何向cookie添加新值?
另外,检查cookie是否存在的首选方法是什么?似乎#has_key?方法不可用。
Where do I made an error?. How should I add new values to the cookie? Additionally, what is the prefered way to check the existance of a cookie? It seems that the #has_key? method is not available.
一些附带问题:
因为我要显示来宾项目的链接(存储在Cookie中) ,其形式为:
Since I want to display links to the guest's items (stored in the cookie), which would have the form of:
link_to @item.title, item_path(@item.id)
然后我应该在#create动作中创建链接并将其存储在cookie中吗?还是应该仅保留我所知道的ID来生成它们?我担心cookie可能无法存储此类链接(因此我最初想到的是仅存储ID)
Should I then just make the links in the #create action and store them in the cookie? Or should I keep just the IDs as I do know, to generate them? I am concerned that a cookie might not be able to store such links (thus I initially thought of storing just IDs)
预先感谢
编辑:似乎不可能将cookie用作数组,并且解决方法是使用某种形式的序列化。我选择了JSON
Seems like it is not possible to use a cookie as an array, and the workaround is to use some form of serialization. I settled on JSON
module SessionHelper
def set_user_cookie
cookies.signed[:token] = {
:value => [].to_json,
:expires => 1.hour.from_now
}
end
def user_cookie_values
load_user_cookie_data
end
def user_cookie_exists?
cookies[:token].present?
end
def add_to_user_cookie(value)
current = load_user_cookie_data
current << value
cookies.signed[:token] = current.to_json
end
def remove_from_user_cookie(value)
current = load_user_cookie_data
current.delete(value)
cookies.signed[:token] = current.to_json
end
private
def load_user_cookie_data
JSON.load(cookies.signed[:token])
end
end
尽管我仍然会对听到在cookie中存储内容的首选方式感兴趣。纯粹的ID或链接?
Although I'd still be interested in hearing what is the preferred way of storing content in the cookie. Pure IDs or Links?
推荐答案
我最近遇到了一个类似的问题,并通过创建一个隐藏所有cookie的CookieCollection类来解决了该问题哈希交互,并允许您像使用数组一样从集合中添加和列出。通过控制器方法和辅助程序公开。
I recently ran into a similar issue and solved it by creating a CookieCollection class that hides all the cookie hash interaction and allows you to add and list from the collection like you would with an Array. It's exposed through a controller method and helper.
http://www.swards.net/2013/08/cookie-collection-objects.html
这篇关于在Rails cookie中存储/检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!