Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zedtux opened this issue Nov 16, 2017 · 3 comments

Comments

@zedtux
Copy link

zedtux commented Nov 16, 2017

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.

@zedtux
Copy link
Author

zedtux commented Nov 16, 2017

I finally found a way to make it working ... not perfect but working.

In my controller I set the :cache_path:

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

  def index
    ...
  end
end

And then I expire the fragment myself in my Sweeper:

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

  def after_update(import)
    ActionController::Base.new.expire_fragment('formulas')
  end
end

dgmstuart added a commit to dgmstuart/swing-out-london that referenced this issue Apr 6, 2018
Was getting "NoMethodError: undefined method `expire_action' " in specs,
despite including the new gems.

This is a hack to get around it, exploiting the fact that pages and
actions seem to be implemented as fragments under the hood:

rails/actionpack-action_caching#54 (comment)
dgmstuart added a commit to dgmstuart/swing-out-london that referenced this issue Apr 6, 2018
Was getting "NoMethodError: undefined method `expire_action' " in specs,
despite including the new gems.

This is a hack to get around it, exploiting the fact that pages and
actions seem to be implemented as fragments under the hood:

rails/actionpack-action_caching#54 (comment)
dgmstuart added a commit to dgmstuart/swing-out-london that referenced this issue May 23, 2018
Was getting "NoMethodError: undefined method `expire_action' " in specs,
despite including the new gems.

This is a hack to get around it, exploiting the fact that pages and
actions seem to be implemented as fragments under the hood:

rails/actionpack-action_caching#54 (comment)
dgmstuart added a commit to dgmstuart/swing-out-london that referenced this issue May 23, 2018
Was getting "NoMethodError: undefined method `expire_action' " in specs,
despite including the new gems.

This is a hack to get around it, exploiting the fact that pages and
actions seem to be implemented as fragments under the hood:

rails/actionpack-action_caching#54 (comment)
@zedtux
Copy link
Author

zedtux commented Jul 18, 2018

Thank you @dgmstuart for your contributions and @Angelmmiguel for merging the PR. Does this mean this issue can be closed ?

@Angelmmiguel
Copy link

@zedtux yes, I think you can close it. The only difference with the code above is to use self.controller= instead of @controller:

self.controller ||= ActionController::Base.new

Btw, the PR was merged but the new version is not released yet.

dgmstuart added a commit to dgmstuart/swing-out-london that referenced this issue Jul 29, 2018
Was getting "NoMethodError: undefined method `expire_action' " in specs,
despite including the new gems.

This is a hack to get around it, exploiting the fact that pages and
actions seem to be implemented as fragments under the hood:

rails/actionpack-action_caching#54 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants