How do I render a partial to a string?

前端 未结 1 1501
日久生厌
日久生厌 2021-01-01 16:19

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

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 16:29

    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
    

    0 讨论(0)
提交回复
热议问题