RABL realmente puede renderizar matrices y matrices de rubíes fácilmente, como atributos, simplemente no como el objeto raíz.Así, por ejemplo, si crea una OpenStruct así por el objeto raíz:
@my_object = OpenStruct.new
@my_object.data = {:c => {:d => 'e'}}
entonces se podría utilizar esta plantilla RABL:
object @my_object
attributes :data
Y hasta la inutilidad:
{"data": {"c":{"d":"e"}} }
Alternativamente, si quiere que :c
sea una propiedad de su objeto raíz, puede usar "nodo" para crear ese nodo y renderizar el hash dentro de ese nodo:
# -- rails controller or whatever --
@my_hash = {:c => {:d => :e}}
# -- RABL file --
object @my_hash
# Create a node with a block which receives @my_hash as an argument:
node { |my_hash|
# The hash returned from this unnamed node will be merged into the parent, so we
# just return the hash we want to be represented in the root of the response.
# RABL will render anything inside this hash as JSON (nested hashes, arrays, etc)
# Note: we could also return a new hash of specific keys and values if we didn't
# want the whole hash
my_hash
end
# renders:
{"c": {"d": "e"}}
Por cierto, este es exactamente el mismo que acaba de usar render :json => @my_hash
en los carriles, por lo RABL no es particularmente útil en este caso trivial;) Pero demuestra la mecánica de todos modos.
Parece que no tiene que usar la notación de puntos si ajusta el hash en un OpenStruct. https://github.com/nesquena/rabl/wiki/Rendering-hash-objects-in-rabl – jmosesman