【问题标题】:Rails fresh_when should include current user id in etagRails fresh_when 应该在 etag 中包含当前用户 ID
【发布时间】:2020-03-20 19:21:21
【问题描述】:

我看到很多这样的例子:

def show
  @article = Article.find(params[:id])
  fresh_when(@article)
end

但是,该页面还包含有关已登录用户的信息(如顶部导航)。用户可以:

  • 以用户A登录
  • 访问文章
  • 退出
  • 以用户 B 登录
  • 再次访问文章

...糟糕,用户将看到有关用户 A 的数据,而不是用户 B,因为文章没有被修改。

如何在哈希 (etag) 中包含当前用户 ID?或者,是否有任何其他解决方案可以避免上述问题?

【问题讨论】:

    标签: ruby-on-rails etag http-caching ruby-on-rails-6


    【解决方案1】:

    也许您可以将 current_user id 添加到所有控制器的 etag 中。

    class ApplicationController < ActionController::Base
      etag { current_user.try :id }
    end
    

    这样,您可以解决登录用户可能获得与未登录用户不同的内容的问题。

    【讨论】:

      猜你喜欢
      • 2012-06-12
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      相关资源
      最近更新 更多