Rails: Cache Sweepers and Expiring Fragments -
i have form admin can create new user, in form there remote: true
. when user created section on page displays of users updated. not happening in production since 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) %>")
not working because have following cache fragment in view:
<% cache('all_school_admin_athletes') %> <% users.each |row_users| %> <div class="row"> <% row_users.each |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 should running. config.active_record.observers = :user_sweeper
anyone else run issue?
figured out...
forgot add controller:
cache_sweeper :user_sweeper, only: [:create]
Comments
Post a Comment