I find myself trying to implement an imperfect solution to not break existing code.
I have an existing helper method in a Rails helper class that selectively renders one of several partials based on a parameter:
def render_widget_container(thingy)
if thingy.is_awesome?
render(partial: 'thingies/awesome', locals: {thingy: thingy })
elsif thingy.sucks?
render(partial: 'thingies/sucky', locals: {thingy: thingy })
end
end
In a controller, I would like to capture the output from this helper, and put it as one of the values in a JSON hash like so:
@thingy = Thingy.find(params[:id])
respond_to do |format|
format.json {
{:name => "thingy 1", :html => render_widget_container(thingy) }
}
end
I have tried the following with these results:
render_widget_container(thingy)
Render and/or redirect were called multiple times in this action.
capture(render_widget_container(thingy))
(eval):1: syntax error, unexpected $undefined $["<div id=\"video_container\"... ^ (eval):1: syntax error, unexpected ']', expecting $end ...r'></div>\n </div>\n</div>\n"] = ["<DIV ID=\"VIDEO_CONTAINE... ...
capture(render_to_string(render_widget_container(thingy)))
*SHIT TON OF ESCAPED HTML* is not an ActiveModel-compatible object that returns a valid partial path.
Questions aside about WHY you would want to do such a thing, how would I go about capturing the generated HTML from my helper, inside my controller?