I have a form where an admin can create a new user, in this form there is a remote: true
. When the user is successfully created the section on the page that displays all of the users is updated. This is not happening in Production since I have caching turned on...
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 %>
UserSweeper:
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
In production $('.<%= type %>-container').html("<%= j render \"school_admin/#{type}/all\", users: @users.in_groups_of(3, false) %>")
this is not working because I have the following cache fragment in the view:
<% 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
Anyone else run into this issue?
Figured it out...
Forgot to add this to my Controller:
cache_sweeper :user_sweeper, only: [:create]