读取Rails中的Cookie值 [英] Reading cookie value in Rails

查看:99
本文介绍了读取Rails中的Cookie值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中成功设置了Cookie:

  var date = new Date(); 
date.setTime(date.getTime()+(1 * 24 * 60 * 60 * 1000)); //一天到期日期
var expires =; expires =+ date.toGMTString();
window.name = date.getTime();
document.cookie =window_name =+ window.name + expires +; path = /;

然后在rails中我尝试读取(我试过以下两个):

  cookies [:window_name] 
request.cookies ['window_name']
/ pre>

两者都有一个空值。
我如何访问我在Javascript中设置的window_name cookie?

解决方案

在rails侧没有值的cookie ...



似乎用JavaScript设置的cookie需要在你的控制器的路径中。



假设您想在用户控制器中使用 cookies [:window_name] ,您需要执行以下操作:

  document.cookie =window_name =+ window.name + expires +; path = / users /; 

必须是安全的东西...
我不会做你能做的,如果你想要在几个控制器中使用该cookie,幸运的是我不!


I successfully set a cookie in JavaScript:

var date = new Date();
date.setTime(date.getTime()+(1*24*60*60*1000)); //one day expiration date
var expires = "; expires="+date.toGMTString();
window.name = date.getTime();
document.cookie = "window_name="+window.name+expires+"; path=/";

Then in rails I try to read (I've tried both of the following):

cookies[:window_name]
request.cookies['window_name']

both of which have an empty value. How can I access the window_name cookie that I set in the Javascript?

解决方案

I had exactly the same problem, cookie with no value on the rails side...

It seems that cookies set with JavaScript need to be in the path of your controller.

Let say you want to use cookies[:window_name] in the users controller, you need to do :

document.cookie = "window_name="+window.name+expires+"; path=/users/";

Must be security stuff... I don't what you could do if you want to use that cookie in several controllers, luckily I don't !

这篇关于读取Rails中的Cookie值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆