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

前端 未结 3 1651
广开言路
广开言路 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:23

    In Rails 6 I think this might be a little different from the accepted answer. I don't think you need to set the underscore in the partial name. This worked for me:

    format.json {
      html_content = render_to_string(partial: 'admin/pages/content', locals: { page: @page }, layout: false, formats: [:html])
      render json: { attachmentPartial: html_content }
    }
    

提交回复
热议问题