How do I render a partial of a different format in Rails?

James A. Rosen picture James A. Rosen · Dec 4, 2008 · Viewed 75.6k times · Source

I'm trying to generate a JSON response that includes some HTML. Thus, I have /app/views/foo/bar.json.erb:

{
  someKey: 'some value',
  someHTML: "<%= h render(:partial => '/foo/baz') -%>"
}

I want it to render /app/views/foo/_baz.html.erb, but it will only render /app/views/foo/_baz.json.erb. Passing :format => 'html' doesn't help.

Answer

Tim Haines picture Tim Haines · Feb 19, 2012

Beginning with Rails 3.2.3, when calling render :partial (only works outside of the respond_to block).

render formats: [ :html ]

instead of

render format: 'html'