1

My code is as follows:

ac = ActionController::Base.new()
# do some stuff
ac.render_to_string(
  partial: my_partial,
  formats: my_format,
  locals: { some_local: some_value }
)

What I would like to do is make some helpers dynamically available to the partial that I render. Ideally, I'd like to loop over an array of helper modules and extend the view so that my helper methods are accessible inside the view. Is this possible?

(I know it's not clean code, but this is a very particular low level service)

Mat
  • 952
  • 2
  • 11
  • 28
  • This is what you're looking for: http://stackoverflow.com/questions/28332630/rails-render-view-from-outside-controller/33349408#33349408 – Damien Roche Nov 21 '16 at 16:50
  • I can't use a view since I need the render_to_string method from a controller object. – Mat Nov 21 '16 at 17:22

1 Answers1

0

Create your own render controller.

module Core
  class RenderController < ActionController::Base
    include Core::ApplicationHelper
    include ::YourCustomHelper
  end
end

then use it for render_to_string

::Core::RenderController.new.render_to_string('agreement_files/shared/a.html.erb', locals:  { a: @a , b: @b })
Jin Lim
  • 1,759
  • 20
  • 24
  • 6 years later, I don't remember what my use case exactly was, but this does look like a good solution :) I'll accept it ;) – Mat May 23 '22 at 08:02