【问题标题】:Rails: Cache Sweepers and Expiring FragmentsRails:缓存清扫器和过期片段
【发布时间】:2013-08-08 01:36:33
【问题描述】:

我有一个管理员可以创建新用户的表单,在这个表单中有一个remote: true。成功创建用户后,页面上显示所有用户的部分将更新。这在生产中没有发生,因为我打开了缓存...

create.js.erb:

<% if @user.valid? %>
    <% type = (@user.type == "Athlete" ? "athletes" : "high_school_coaches" ) %> 
      alert("<%= @user.email %> created!");
      $('form#new_user').find('input:not(:hidden, :submit)').val('');
      $('.<%= type %>-container').html("<%= j render \"school_admin/#{type}/all\", users: @users.in_groups_of(3, false) %>")
<% else %>
  alert("<%= @user.errors.full_messages.to_sentence %>");
<% end %>

用户扫描器:

class UserSweeper < ActionController::Caching::Sweeper
  observe Athlete, HighSchoolCoach

  def after_create(record)
    expire_cache(record)
  end

  def after_update(record)
    expire_cache(record)
  end

  def expire_cache(record)
    type = record.is_a?(HighSchoolCoach) ? "coach" : "athlete"
    expire_fragment("all_school_admin_#{type.pluralize}")
  end
end

在生产中 $('.&lt;%= type %&gt;-container').html("&lt;%= j render \"school_admin/#{type}/all\", users: @users.in_groups_of(3, false) %&gt;") 这不起作用,因为我在视图中有以下缓存片段:

<% cache('all_school_admin_athletes') do %>
  <% users.each do |row_users| %>
    <div class="row">
      <% row_users.each do |user| %>
        <%= render user %>
      <% end %>
    </div>
  <% end %>
<% end %>

应用程序.rb

config.autoload_paths += %W(
  #{config.root}
  #{config.root}/lib
  #{config.root}/app/models
  #{config.root}/app/models/concerns
  #{config.root}/app/sweepers
)

# Activate observers that should always be running.
config.active_record.observers = :user_sweeper

还有其他人遇到过这个问题吗?

【问题讨论】:

    标签: ruby-on-rails-3 fragment-caching sweeper


    【解决方案1】:

    想通了……

    忘记将其添加到我的控制器中:

    cache_sweeper :user_sweeper, only: [:create]

    【讨论】:

      猜你喜欢
      • 2011-04-15
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2012-05-13
      • 1970-01-01
      相关资源
      最近更新 更多