【问题标题】:Rails - cookies value doesnt persist across actionsRails - cookie 值不会在操作中持续存在
【发布时间】:2016-06-06 13:46:14
【问题描述】:

我正在尝试将数据保存在 cookie 中以供以后使用。

def hello
  name = SecureRandom.hex
  cookies[:cookies_name] = name
  .....
end

def process_file
  logger.debug "The number of lines in specific is updr #{cookies[:cookies_name] }"
   ...
end

在action process_file中,我存储在cookies中的值是空格(nil)。此外,当我尝试查看 cookie 时使用开发人员工具(Chrome -> 开发人员工具 -> 资源),我看不到任何 cookie。

如何设置 cookie?

【问题讨论】:

    标签: ruby-on-rails cookies


    【解决方案1】:

    尝试为您的 cookie 设置到期日期

    cookies[:cookies_name] = {value: name, expires: 1.year.from_now}
    

    【讨论】:

    • 您检查过name 变量的值吗?也许那是 nil 所以它将是存储到您的 cookie 中的值
    • 嗨,Lymuel - 变量“名称”不为零。我可以在日志文件中看到 name 的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 2022-07-09
    • 2015-06-15
    相关资源
    最近更新 更多