我正在遵循一种巧妙的方法,为我的Rails应用程序https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user创建来宾用户
我正在使用cookie而不是会话(如下面的代码),以尝试维护每个客户端设备的相同用户,但是,当我关闭浏览器并再次打开时,cookie没有保存,并创建了一个新用户。我已经在Chrome和Firefox上试过了,两者的行为是一样的。
def create_guest_user
u = User.new(:name => "guest", :email => "guest_#{Time.now.to_i}#{rand(100)}@example.com", guest: true)
u.save!(:validate => false)
cookies.signed.permanent[:guest_user_id] = u.id
u
end当我在chrome上检查会话过期时,它的浏览器并没有像它应该的那样永久关闭。我也尝试显式地设置过期日期,但仍然得到相同的问题

有没有人知道这个问题和可能的解决方案?
发布于 2019-01-18 18:51:41
尝尝这个
response.set_cookie(:guest_user_id, {
value: u.id,
path: '/', # cookie remains all pages in this domain
expires: 2.weeks.from_now, # cookie expires after 2 week
httponly: true, # prevent javascript modify this cookie
})https://stackoverflow.com/questions/54246115
复制相似问题