基于会话ID的片段缓存

时间:2019-02-18 11:23:31

标签: ruby-on-rails

我正在寻找一种基于ruby on rails中的会话ID在特定形式(新操作)上使用片段缓存的方法。这些表格应在会话销毁或这些表格中使用的记录的任何更改时到期。

问题: 这些表单内容根据用户类型而改变,就像某些用户类型显示了某些字段一样。 当我缓存表单时,所有用户类型的登录名都显示相同的表单。

New.html.erb

<% cache 'procedure_form_cache', skip_digest: true do %>
    <div class="container">
        <hr>
        <h3>Procedure</h3>
        <%= render 'form', procedure: @procedure %>
    </div>
<%end%>

Procedure.rb

class Procedure < ApplicationRecord
  def expire_cache             
         ActionController::Base.new.expire_fragment('procedure_form_cache')
  end
end

我希望片段缓存基于表单中的会话ID

1 个答案:

答案 0 :(得分:0)

<% cache [session.id, @procedure.updated_at] do %>
    <div class="container">
        <hr>
        <h3>Procedure</h3>
        <%= render 'form', procedure: @procedure %>
    </div>
<%end%>

应该可以解决,如果会话ID发生更改或@procedure发生任何更改,缓存将被更新。