How to call expire_fragment from Rails Observer/Model?

前端 未结 8 798
长发绾君心
长发绾君心 2020-12-23 10:15

I\'ve pretty much tried everything, but it seems impossible to use expire_fragment from models? I know you\'re not supposed to and it\'s non-MVC, but surely there much be

8条回答
  •  有刺的猬
    2020-12-23 10:41

    Will it not be easier and clean just to pass the current controller as an argument to the model method call? Like following:

    def delete_cascade(controller)
    
      self.categories.each do |c|
        c.delete_cascade(controller)
        controller.expire_fragment(%r{article_manager/list/#{c.id}.*})                
      end
      PtSection.delete(self.id)
      controller.expire_fragment(%r{category_manager/list/#{self.id}.*})        
    end
    

    You can access all public methods and properties of the controller from within model. As long as you do not modify the state of the controller, it should be fine.

提交回复
热议问题