[Rails] Re: Need help : How to render HTML inside of a JSON request

2009-05-30 Thread RFine

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.

Russell

On May 30, 11:16 am, Jonathan Rochkind  wrote:
> 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.http://api.rubyonrails.org/classes/ActionController/MimeResponds/Inst...
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Need help : How to render HTML inside of a JSON request

2009-05-30 Thread Jonathan Rochkind

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. :)
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Need help : How to render HTML inside of a JSON request

2009-05-30 Thread Jonathan Rochkind

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. 
http://api.rubyonrails.org/classes/ActionController/MimeResponds/InstanceMethods.html#M000368


-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---