rails 3 - 如何将PARTIAL呈现为Json响应

时间:2021-09-17 18:54:52

I want to do something like this:

我想做这样的事情:

class AttachmentsController < ApplicationController
  def upload
    render :json => { :attachmentPartial => render :partial => 'messages/attachment', :locals => { :message=> @message} }
  end

Is there a way to do this? render a Partial inside a JSON object? thanks

有没有办法做到这一点?在JSON对象中渲染Partial?谢谢

2 个解决方案

#1


42  

This should work:

这应该工作:

def upload
    render :json => { :attachmentPartial => render_to_string('messages/_attachment', :layout => false, :locals => { :message => @message }) }
end

Notice the render_to_string and the underscore _ in before the name of the partial (because render_to_string doesn't expect a partial, hence the :layout => false too).

注意部分名称之前的render_to_string和下划线_in(因为render_to_string不期望部分,因此:layout => false)。


UPDATE

UPDATE

If you want to render html inside a json request for example, I suggest you add something like this in application_helper.rb:

例如,如果你想在json请求中呈现html,我建议你在application_helper.rb中添加这样的东西:

# execute a block with a different format (ex: an html partial while in an ajax request)
def with_format(format, &block)
  old_formats = formats
  self.formats = [format]
  block.call
  self.formats = old_formats
  nil
end

Then you can just do this in your method:

然后你可以在你的方法中执行此操作:

def upload
  with_format :html do
    @html_content = render_to_string partial: 'messages/_attachment', :locals => { :message => @message }
  end
  render :json => { :attachmentPartial => @html_content }
end

#2


2  

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.

要在json响应中呈现html部分,实际上并不需要使用with_format帮助器,如mbillard的答案中所述。您只需在render_to_string调用中指定格式,如格式:: html。

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

#1


42  

This should work:

这应该工作:

def upload
    render :json => { :attachmentPartial => render_to_string('messages/_attachment', :layout => false, :locals => { :message => @message }) }
end

Notice the render_to_string and the underscore _ in before the name of the partial (because render_to_string doesn't expect a partial, hence the :layout => false too).

注意部分名称之前的render_to_string和下划线_in(因为render_to_string不期望部分,因此:layout => false)。


UPDATE

UPDATE

If you want to render html inside a json request for example, I suggest you add something like this in application_helper.rb:

例如,如果你想在json请求中呈现html,我建议你在application_helper.rb中添加这样的东西:

# execute a block with a different format (ex: an html partial while in an ajax request)
def with_format(format, &block)
  old_formats = formats
  self.formats = [format]
  block.call
  self.formats = old_formats
  nil
end

Then you can just do this in your method:

然后你可以在你的方法中执行此操作:

def upload
  with_format :html do
    @html_content = render_to_string partial: 'messages/_attachment', :locals => { :message => @message }
  end
  render :json => { :attachmentPartial => @html_content }
end

#2


2  

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.

要在json响应中呈现html部分,实际上并不需要使用with_format帮助器,如mbillard的答案中所述。您只需在render_to_string调用中指定格式,如格式:: html。

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