RABL può effettivamente rendere facilmente gli hash e gli array di ruby, come attributi, ma non come oggetto radice.Così, per esempio, se si crea un OpenStruct come questo per l'oggetto root:
@my_object = OpenStruct.new
@my_object.data = {:c => {:d => 'e'}}
allora si potrebbe utilizzare questo modello Rabl:
object @my_object
attributes :data
E questo renderebbe:
{"data": {"c":{"d":"e"}} }
In alternativa, se si desidera che :c
sia una proprietà dell'oggetto radice, è possibile utilizzare "nodo" per creare quel nodo e rendere l'hash all'interno di tale 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"}}
Incidentalmente, questo è esattamente lo stesso come usando solo render :json => @my_hash
a rotaie, quindi Rabl non è particolarmente utile in questo caso banale;) ma dimostra comunque la meccanica.
Sembra che non sia necessario utilizzare la notazione a punti se si avvolge l'hash in un OpenStruct. https://github.com/nesquena/rabl/wiki/Rendering-hash-objects-in-rabl – jmosesman