1

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:

  1. render_widget_container(thingy)

    Render and/or redirect were called multiple times in this action.

  2. 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...
    ...    
    
  3. 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?

thoughtpunch
  • 1,907
  • 4
  • 25
  • 41
  • 2
    Maybe you want to use "render_to_string"? (http://stackoverflow.com/questions/13713250/render-to-string-partial-format-error-in-controller) – MrYoshiji Jun 26 '13 at 15:40

1 Answers1

0

Rather than try to capture the rendered output in the controller, I simply capture the render action in the view. Not an exact solution to my original question, but functional.

thoughtpunch
  • 1,907
  • 4
  • 25
  • 41