Rails片段缓存无法正常工作

时间:2014-08-09 05:34:28

标签: ruby-on-rails ruby caching ruby-on-rails-4

我在视图中有这段代码

<% cache("students_cache") do %>
  <% @swimming_students.each do |swimming_student| %>
    <tr class="gradeX">
      <td><%= link_to " #{swimming_student.full_name}", swimming_student %></td>
      <td><%= swimming_student.homephone %></td>          
    </tr>
  <% end %>
<% end %>

我在anther action中有过期代码

  def create
     expire_fragment("students_cache")
    @swimming_student = Swimming::Student.new(params[:swimming_student])

    respond_to do |format|
      if @swimming_student.save
        format.html { redirect_to @swimming_student, notice: 'Student was successfully created.' }
        format.json { render json: @swimming_student, status: :created, location: @swimming_student }
      else
        format.html { render action: "new" }
        format.json { render json: @swimming_student.errors, status: :unprocessable_entity }
      end
    end

但在我创建新学生后,缓存未更新。

缺少什么?

1 个答案:

答案 0 :(得分:3)

使用带字符串键的片段缓存时,应关闭缓存摘要:

<% cache("students_cache", skip_digest: true) do %>

有关详细信息,请参阅this Stackoverflow question