How do I render a partial to a string, so I can include it as part of a JSON response? I have to put it into a JSON response to allow room for a possible error message. The
Ok I got it. I had to add render json:
. I thought I had tried that before and it gave me a double render error. I guess when you render_to_string
, then it's ok to have more than one render.
respond_to do |format|
format.html { redirect_to post_path(post) }
format.js {
render json: {
error: flash[:error],
content: (render_to_string partial: '/comments/comment', locals: {comment: comment}, layout: false )
}
}
end