You can clone with
When I do the following:
json.partial! "comment", :collection => comments
I expect this to be equivalent to
json.array!(comments) do |json, comment|
json.partial! "comment", :comment => comment
Instead, jbuilder tries to render the partial once with collection as a local variable
This is now supported in this commit: e2a6616 and is available in gem version 0.4.3.
It allows you to supply a hash with the collection:
json_partial! partial: "projects/index", collection: @projects
I'm not sure this is working correctly. Passing a collection to json.partial! works - but only renders the last item in the array.
Edit: from debugger and log output, it looks like the partial is getting processed for each item in the collection. But jbuilder output only returns the last item in the collection.
To expand a little on what @mattgrayson found, it is processing the collection elements and merging them into the template, but because the template is based on a hash, the properties of each object in the collection are replacing the value of the previous one. The assumption seems to be that rendering with a collection expects the rendered partials can safely be appended to the view, which is ok for html/erb, but this breaks with jbuilder.
I looked into adding this into jbuilder and it would seem to have to duplicate a lot of what rails is doing in it's renderers. IMO, I'm not sure it's worth supporting it when you can use the array method.
We are not going to support this at this time. You'll just need to loop yourself.