0

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

创建.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

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

<% 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

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

4

1 回答 1

0

弄清楚了...

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

cache_sweeper :user_sweeper, only: [:create]

于 2013-08-08T02:43:42.413 回答