title: In Rails 3, how does one render HTML within a JSON response?
body: <p>I'm porting an application from Merb 1.1 / 1.8.7 to Rails 3 (beta) / 1.9.1 that uses JSON responses containing HTML fragments, e.g., a JSON container specifying an update, on a user record, and the updated user row looks like . In Merb, since whatever a controller method returns is given to the client, one can put together a Hash, assign a rendered partial to one of the keys and return hash.to_json (though that certainly may not be the best way.) In Rails, it seems that to get data back to the client one must use render and render can only be called once, so rendering the hash to json won't work because of the partial render.</p>
  
  <p>From reading around, it seems one could put that data into a JSON .erb view file, with &lt;%= render partial %> in and render that. Is there a Rails-way of solving this problem (return JSON containing one or more HTML fragments) other than that?</p>
  
  <p>In Merb:<br>
  controller:</p>
  
  <pre><code>only_provides :json
  ...
  self.status = 204 # or appropriate if not async
  return {
      'action' =&gt; 'update',
        'type' =&gt; 'user',
          'id' =&gt; @user.id,
        'html' =&gt; partial('user_row', format: :html, user: @user)
  }.to_json
  </code></pre>
  
  <p>In Rails:<br>
  controller:</p>
  
  <pre><code>respond_to do |format|
    format.json do
      render template: '/json/message-1', 
        locals: { 
          action: 'update',
          type: 'user',
          id: @user.id,
          partial: 'user_row.html.erb', 
          locals: { user: @user }
        }
    end
  end
  </code></pre>
  
  <p>view: json/message-1.json.erb</p>
  
  <pre><code>{
    "action": &lt;%= raw action.to_json %&gt;,
    "type": &lt;%= raw type.to_json %&gt;,
    "id": &lt;%= raw id.to_json %&gt;,
    "html": &lt;%= raw render(partial: partial, locals: locals).to_json %&gt;
  }
  </code></pre>