我有一个表单,管理员可以在其中创建一个新用户,在这个表单中有一个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
在生产$('.<%= 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 %>
Application.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
还有人遇到这个问题吗?
想好了。。。
忘记将此添加到我的控制器:
cache_sweeper :user_sweeper, only: [:create]