RABLは実際にルビのハッシュと配列を属性として簡単にルートオブジェクトとしてレンダリングすることはできません。
@my_object = OpenStruct.new
@my_object.data = {:c => {:d => 'e'}}
次にあなたがこのRABLテンプレートを使用することができます:だから、例えば、あなたがルートオブジェクトのためのこのようなOpenStruct作成した場合
object @my_object
attributes :data
をそして、それはレンダリングされます:
{"data": {"c":{"d":"e"}} }
を
また、:c
をルートオブジェクトのプロパティにする場合は、 "node"を使用してそのノードを作成し、そのノード内でハッシュをレンダリングできます。
# -- 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"}}
ちなみに、これはまさにちょうどレールでrender :json => @my_hash
を使用するのと同じであるので、RABLはこの些細な場合に特に有用ではありません。)しかし、それはとにかく仕組みを示しています。
ハッシュをOpenStructでラップする場合は、ドット表記法を使用する必要はありません。 https://github.com/nesquena/rabl/wiki/Rendering-hash-objects-in-rabl – jmosesman