n8 / multi_fetch_fragments

Multi-fetch Fragments makes rendering and caching a collection of template partials easier and faster.
http://ninjasandrobots.com/rails-faster-partial-rendering-and-caching
MIT License
539 stars 63 forks source link

Error With Instantiated ActionView #24

Closed neilchaudhuri closed 11 years ago

neilchaudhuri commented 11 years ago

Ruby 1.9.3/Rails 3.2

I am creating a PDF within a delayed_job job, and to do this I am generating HTML content within the job. This means I cannot rely on one of the provided controllers and must instantiate my own ActionView:

action_view = ActionView::Base.new(Rails.configuration.paths["app/views"]) action_view.class_eval do include Rails.application.routes.url_helpers include ApplicationHelper

  def protect_against_forgery?
    false
  end

end action_view.render(:template => 'presentations/print_selections', :layout => nil, :format => 'html', :locals => { :presentation => presentation, :title => title, :selector => selector })

The template listed above contains a call to multi_fetch_fragments. If I add a hardcoded string to a custom cache_key like so

<%= render partial: 'presentations/cache_partials/pdf/talent', collection: category.talents, cache: Proc.new { |talent| [talent, 'pdf'] }, :locals => {:presentation => presentation, :selector => selector} %>

I get this error:

Error message: undefined method fragment_cache_key' for nil:NilClass /app/vendor/bundle/ruby/1.9.1/gems/multi_fetch_fragments-0.0.16/lib/multi_fetch_fragments.rb:36:inblock in render_collection_with_multi_fetch_cache' /app/vendor/bundle/ruby/1.9.1/gems/multi_fetch_fragments-0.0.16/lib/multi_fetch_fragments.rb:24:in each' /app/vendor/bundle/ruby/1.9.1/gems/multi_fetch_fragments-0.0.16/lib/multi_fetch_fragments.rb:24:inrender_collection_with_multi_fetch_cache' /app/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.11/lib/action_view/renderer/partial_renderer.rb:234:in block in render' /app/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.11/lib/action_view/renderer/abstract_renderer.rb:38:inblock in instrument'

If I use the default cache_key then it works.

I tried adding a variable key (rather than a hardcoded string) and found failure as well.

Any reason why a custom action view would cause errors with custom keys?

Thanks.

n8 commented 11 years ago

I think this commit is going to solve your problems.

https://github.com/n8/multi_fetch_fragments/commit/1a5e2e9d791cfb8314c1129950514c3eb2fe753f

Would you be able to write a test for the problem you saw to include in this project?