加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 为什么这个缓存语句没有写出我期望的密钥?

发布时间:2020-12-17 03:10:36 所属栏目:百科 来源:网络整理
导读:我在_profile.html.erb部分中有这个: % cache [current_user.roles.first,profile,@selected_profile,params[:rating]] do % 然而,这是我在服务器日志中看到的: Read fragment views/profiles/26-20161212033839290582/profiles/52-20161213010040474070/p
我在_profile.html.erb部分中有这个:

<% cache [current_user.roles.first,profile,@selected_profile,params[:rating]] do %>

然而,这是我在服务器日志中看到的:

Read fragment views/profiles/26-20161212033839290582/profiles/52-20161213010040474070/profiles/14-20161213015458288839/profiles/34-20161212035644491093/profiles/33-20161212035644237925/profiles/38-20161207092843851446/profiles/35-20161212040016291016/profiles/36-20161212040016565707/profiles/4-20161213021028862933/profiles/39-20161207092843925084/profiles/46-20161207092844067579/profiles/47-20161207223703646028/profiles/37-20161212040016656625/660bdc6ad0b20e4c5329112cf79946f7 (0.1ms)

我对那里的角色一无所知.

发生的事情是,如果我以具有管理员角色的用户身份登录,然后以具有其他角色的用户身份登录,我看到缓存的配置文件显示为我是管理员而不是其他用户具有正确的视图.

可能导致这种情况的原因以及如何解决?

编辑1

如果我将缓存语句更改为:

<% cache [current_user,params[:rating]] do %>

并刷新,这是日志的样子:

Write fragment views/users/2-20161218005548388099/profiles/37-20161212040016656625///bb163edd4a8c7af2db71a04243338e7b (0.1ms)
  Rendered collection of profiles/_profile.html.erb [1 times] (25.4ms)
Write fragment views/profiles/26-20161212033839290582/profiles/52-20161213010040474070/profiles/14-20161213015458288839/profiles/34-20161212035644491093/profiles/33-20161212035644237925/profiles/38-20161207092843851446/profiles/35-20161212040016291016/profiles/36-20161212040016565707/profiles/4-20161213021028862933/profiles/39-20161207092843925084/profiles/46-20161207092844067579/profiles/47-20161207223703646028/profiles/37-20161212040016656625/83ddeaa031bf68e602ce66af2d268317 (0.1ms)

编辑2

当我绑定到_profile.html.erb时,我得到以下内容:

[3] pry(#<#<Class:0x007f968480ec98>>)> current_user.roles.first
=> #<Role:0x007f969e4422a8 id: 1,name: "admin",resource_id: nil,resource_type: nil,created_at: Mon,12 Sep 2016 00:38:47 UTC +00:00,updated_at: Mon,12 Sep 2016 00:38:47 UTC +00:00>

我甚至尝试了以下方法:

<% cache [current_user.roles.first.name,params[:rating]] do %>

当以管理员用户身份登录为非管理员用户时,它仍然给我相同的缓存结果.

编辑3

以下是调用调用_profile.html.erb的集合的缓存块:

<% cache @profiles do %>
    <div class="wrapper wrapper-content">
    <% @profiles.to_a.in_groups_of(3,false).each do |profiles| %>
        <div class="row">
            <%= render partial: "profile",collection: profiles %>
        </div>
    <% end %>
    </div>
  <% end %>

解决方法

您的父缓存会阻止子缓存正确执行其工作.您可以删除父缓存,也可以将其移到顶部,然后每个子项可以分开

替换<%cache @profiles do%>有:

<% cache [current_user.roles.first.try(:name),@profiles,params[:rating]] do %>

然后是内部_profile.html.erb

<% cache [current_user.roles.first.try(:name),params[:rating]] do %>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读