Re: partials and render_to_string

Frederick Cheung
Mon, 19 Sep 2011 08:26:21 -0000

On Sep 18, 7:40 pm, 7stud --
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:

> > 2) What exactly is the point of render_to_string? Why can''t
we just
> > use "render"? Or do we need to use render_to_string in order
to use
> > the jquery append method, for example, when appending it to the DOM?
>
> According to railsguide:
>
> ... render_to_string. This method takes exactly the same options as
> render, but it returns a string instead of sending a response back to
> the browser.
>
> So render_to_string does not send a response back to the browser, while
> render :text => '' hello world " does
send a response back to
> the browser.
>
One addition to this: it depends if you are writing code in your
controller on in a view. In a view, render just returns a string and
you don''t need render_to_string (I don''t think it even
exists). In a
controller render sets the response body (and calling it a second time
raises a doublerendererror), so if you need to manipulate the rendered
content in anyway (e.g. stick it in a json document) then you need to
use render_to_string

Fred

> --
> Posted viahttp://www.ruby-forum.com/.