Skip to content

Rendering hash objects in rabl

nesquena edited this page Sep 25, 2012 · 2 revisions

RABL deals in objects but does not require a particular ORM. Just objects that support dot notation. If you want to use rabl and all you have is a hash:

@user = { :name => "Bob", :age => 27, :year => 1976 }

then you need to first turn the hash into an object that supports dot notation:

@user = OpenStruct.new({ :name => "Bob", :age => 27, :year => 1976 })

and then within a RABL template treat the OpenStruct as any other object:

object @user
attributes :name, :age, :year

Consider that if everything you are doing in your app is just dealing in hashes and there is no objects or databases involved, you may be better off with an alternative more custom JSON builder such as json_builder or jbuilder.