Arslan Ali Arslan Ali - 1 year ago 204
JSON Question

How to render a collection in jbuilder?

There is a fantastic method in Rails to render a collection of objects for view templates, and it works as fellows:

<%= render @restaurants %>

It's automatically gonna look for a partial named
, loop through collection, pass individual object to the partial, and render it. This is pretty fantastic.

Now, I'm looking for a very similar way in


json.(restaurant, :id, :name, :address, :latitude, :longitude)

Now, for
, how would I use this partial in the same way it works in Rails for view templates? For the time being, I'm rendering the collection the following way:

json.restaurants @restaurants do |restaurant|
json.partial! restaurant

Answer Source

Ok, so I think you are looking for the below :

json.partial! :restaurant, collection: @restaurants
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download