views:

451

answers:

2

I am using render_to_string within a JSON 'render' response. The render_to_string method seems to be striping my HTML, what am I doing wrong here?

Here's a sample:

render :json => {:html => render_to_string(:partial => 'view', :locals => { data => @data} )}

The response is coming through without any markup on it.

+1  A: 

Have you tried setting the template format in the respond_to block like this:

format.json do
  @template.template_format = :html
  @html = render_to_string( :partial => 'view', :locals => { data => @data}  )
  render :json => { :success => true, :html => @html }
end
arnklint
The problem is I'm using this with the Ajax Upload plugin so I don't even have a true response block. This all a bit of hack to be honest. I could just return the data and have the client build the needed HTML but it would be nice to use the partial template.
mikeycgto
A: 

OK, this is such a hack, but if I CGI.escape the render_to_string I can unescape and load in the HTML client-side. I think the issue here is that I'm not telling Rails to render HTML in the first place. In fact, I am using render :text since render :json will cause the browser to treat the file like a download since the Ajax Upload library isn't even using Ajax, rather it uses a form of iframe trickery.

mikeycgto