5

I want to pre-build a partial that takes a few seconds to render. I have seen functions which use proxies to preload the cache via http, but I would like to have an "internal" solution.

This is my function, which is called whenever myobject is updated:

def pre_build_partial myobject
  the_controller = ActionController::Base.new
  the_controller.instance_variable_set '@myobject', myobject

  view_renderer = ActionView::Renderer.new the_controller.lookup_context
  view_renderer.render the_controller.view_context, { partial: 'mypartial', layout: false }
end

It works fine for partials that use basic helpers, but my custom helpers throw errors:

undefined method `my_custom_helper_function' for #<#<Class:...>

I guess the helper has to be included in the_controller, but I can't find a way to do so. Thanks for any help in advance!

Railsana
  • 1,813
  • 2
  • 21
  • 30
  • Can you show me which file you put that codes on? – Hiroaki Machida Jan 14 '15 at 10:09
  • you probably want to `ApplicationController.new` because everything is setup in there already? – phoet Jan 14 '15 at 12:03
  • Phoet's proposal is rather elegant. Not the lightest solution (you would ideally only include the helper you need), but if it works you're moving in the right direction. Please give us an update. – Ecnalyr Jan 14 '15 at 16:03
  • Thank you, phoet! That's a nice solution. Please post your comment as an answer, so I can hand you the bounty. – Railsana Jan 14 '15 at 17:15

1 Answers1

3

You could pick some ideas from the source :) https://github.com/rails/rails/issues/18409

Cristian Bica
  • 4,067
  • 27
  • 28