jeudi 21 juillet 2016

Calling controller method from a helper module

Using Rails 3.2

I'm trying to call expire_fragment, the Rails view method, from a helper but getting an error:

undefined method `expire_fragment' for #<#<Class:0x00000118977110>:0x00000103b853b8>

I'm trying to conditionally clear the cache. This is the helper method call in my view

clear_cache_keys_if(params[:cc], [@product, :search_filters])

And in the helper

  def clear_cache_keys_if(condition, keys = [])
    if condition
      keys.each do |key|
        expire_fragment(key)
      end
    end
  end

I would have thought that the Rails fragment caching methods would be accessible in a helper module, but that doesn't seem to be the case.

I changed it to

controller.expire_fragment(key) 

And that worked.

This method is available in the view. It's available from the controller. I don't really understand why it is not available in a helper. What am I missing here? Why isn't it available in the helper and what is the best way to expose it? Thanks

Aucun commentaire:

Enregistrer un commentaire