Skip to content

NoMethodError: undefined method `expire_action' for #<FormulasSweeper:0x00000004e935f0 @controller=nil> #54

Closed
@zedtux

Description

@zedtux

I have the same issue but in my running application (not RSpec) with rails 5.1.4.

In my controller:

class FormulasController < ApplicationController
  ...
  caches_action :index
  cache_sweeper :formulas_sweeper, only: :index

  def index
    ...
  end
end

And my Sweeper class:

class FormulasSweeper < ActionController::Caching::Sweeper
  observe Import

  def after_update(import)
    expire_action controller: 'formulas', action: 'index'
  end
end

The Sweeper is running and failing with the following error when I'm updating an Import from the Rails console, or from a Sidekiq job:

NoMethodError: undefined method `expire_action' for #<FormulasSweeper:0x00000004e935f0 @controller=nil>
	from app/sweepers/formulas_sweeper.rb:6:in `after_update'
	from (irb):2

@nnattawat's fix from rails/rails-observers#31

Updating my Sweeper as following:

class FormulasSweeper < ActionController::Caching::Sweeper
  observe Import

  def after_update(import)
    @controller ||= ActionController::Base.new
    expire_action controller: 'formulas', action: 'index'
  end
end

Makes the code fail with:

NoMethodError: undefined method `host' for nil:NilClass
	from app/sweepers/formulas_sweeper.rb:7:in `after_update'
	from (irb):2

This is due to the fact that ActionController::UrlFor.url_options is expecting a request object which is nil in my case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions