I'm working with Rabl for a while and just today I faced an interesting problem that I could't solve quite well..
So, I have a collection returned from GET ".../list/512/resources" and here is my sample rabl template that I used to return (without root) :
collection @resources
extends "api/v1/resources/_base"
=> { [ {...}, {...}, {...} ] }
But, now I realize that I want to return different templates for each resource depending on their attributes.. so that's easy right?
node @resources => :resources do |resource|
if resource.type == 'Document'
partial('...', :object => resource)
elsif @resource.type == 'Folder'
partial('...', :object => resource)
end
end
=> { resources: [ {...}, {...}, {...} ] }
But ohh! Now I don't want that "resources" node there.. how should it be done? I tried something like:
array = []
@resources.each do |resource|
if resource.type == 'Document'
array << partial('...', :object => resource)
elsif @resource.type == 'Folder'
array << partial('...', :object => resource)
end
end
collection array
but no success, it returns empty objects like => [ {}, {}, {} ]. Any idea how can I accomplish that?