Forum: Ruby on Rails Need help : How to render HTML inside of a JSON request

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Aea0903275adb450272c2fbd4bd41f47?d=identicon&s=25 RFine (Guest)
on 2009-05-30 18:55
(Received via mailing list)
We have an app that is in production with Rails 2.1.2 and I'm about to
upgrade to 2.3.2.  However, I am having a problem with a number of
AJAX calls and I could really use some help.  The client will make an
AJAX call to receive a json response, and the response includes an
html string as one member of the json data object.

We render the html to a string (render_to_string).  and return the
object.  However, in the 2.3.2 version, we are receiving an erorr that
some of the partials called inside of the render can't be found.  I've
isolated it to the partials that aren't fully qualified names.  For
example, this appears to fail:

render :partial => 'comments/partial_name'

If I change it to :

render :partial => 'comments/partial_name.html.erb'

and it works. It appears as though the fact that the call is a JSON
request from the client is causing the render engine not to search
for .html and .html.erb in the path, thus they fail.

Any ideas?

0f50b9a2ad85666d537d39bda49327ee?d=identicon&s=25 Jonathan Rochkind (jrochkind)
on 2009-05-30 20:16
RFine wrote:

> render :partial => 'comments/partial_name'
> If I change it to :
> render :partial => 'comments/partial_name.html.erb'
> and it works. It appears as though the fact that the call is a JSON
> request from the client is causing the render engine not to search
> for .html and .html.erb in the path, thus they fail.

You got it. Why are you returning .html from a ajax/json request from
the client, anyway?  Generally, ajax requests from the client get back
javascript (or specifically json), not html!

Rails is trying to help you with this general case, because you can have
the same action that will return partial.html.erb if it's an html
request, or partial.js.erb if it's an AJAX request.  I forget exactly
how Rails determines when the format is js, but I know if you're using
any of the Rails helper methods for ajax callbacks, they definitely
include a query param to tell Rails what's what.

So you can change the render call like you say. Or you COULD rename the
partial in the old way partial_name.erb, so Rails will use it for any
format request. But that's kind of weird (and now i'm not entirely sure
it'll work).  Or you could reconsider why you want to return HTML
(rather than js/json) to a js request in the first place. Or you could
take a look at respond_to to see how you can return different views for
different request types.
0f50b9a2ad85666d537d39bda49327ee?d=identicon&s=25 Jonathan Rochkind (jrochkind)
on 2009-05-30 20:18
Bah, you know what, ignore me. I just realized you're asking about
_partial_ specifically, and now I realize the solution isn't as clear.
And I know why you might want an html partial in a js response, because
I do that myself. :) And now I see that I'll have to deal with this when
I upgrade to rails 3 too. I suspect you just have to change the partial
like you say, but I don't know if that would be considered a bug or not.

Okay, I'm done in my marathon of answering quesitons in the listserv,
now that I've started giving bad answers. :)
Aea0903275adb450272c2fbd4bd41f47?d=identicon&s=25 RFine (Guest)
on 2009-05-31 00:41
(Received via mailing list)
Thanks, Jonathan.  First off, I inherited this code base and I realize
that there is a better solution... specifically to do a request with
update.  The request ask for json because they return both data
elements which are used in the page and an html block as a replacement
for a section.  If there were only one call... I would just fix it by
hand, but this approach has been used in multiple places in the code
base.  They make a call that constructs a json object, and one element
in it is a string of html to replace a div.  Unfortunately, when we
call render_string -- now the render wont look for .html.erb objects.
I've tried adding :content_type=:html to the render call, to no
avail.  I've also tried adding Mime::HTML and 'text/html'
as :content_type parameters.

I think I just need to temporarily override the request content type
during rendering, but I can't figure out how.


On May 30, 11:16 am, Jonathan Rochkind <rails-mailing-l...@andreas-
This topic is locked and can not be replied to.