The Spaghetti Refactory Established 2015

Pitfalls when using Rails `render_to_string`

I spent a couple hours debugging this one and thought I should write it down:

#render_to_string needs the full file extension for templates and partials passed in, and it also needs locals defined even if the local is an exposed variable or an instance variable.

For example, the approach detailed in the previous post:

object_details = JSON.parse render_to_string(partial: 'path/to/object/detail.json.jbuilder')

It needs locals defined in order to work properly:

object_details = JSON.parse render_to_string(partial: 'path/to/object/detail.json.jbuilder', locals: { object: object })

Also need to ensure if there are any nested partials within the partial being rendered to string, those partials need the file extension as well.