rails 3 - How to render a PARTIAL as a Json response

前端 未结 3 1662
广开言路
广开言路 2020-12-25 12:04

I want to do something like this:

class AttachmentsController < ApplicationController
  def upload
    render :json => { :attachmentPartial => rende         


        
3条回答
  •  时光取名叫无心
    2020-12-25 12:36

    This question is a bit old, but I thought this might help some folks.

    To render an html partial in a json response, you don't actually need the with_format helper as explained in mbillard's answer. You simply need to specify the format in the call to render_to_string, like formats: :html.

    def upload
      render json: { 
        attachmentPartial: 
          render_to_string(
            partial: 'messages/attachment', 
            formats: :html, 
            layout: false, 
            locals: { message: @message }
          )
      }
    end
    

提交回复
热议问题