nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support
http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/
MIT License
3.64k stars 334 forks source link

Unable to properly render json for an simple array of objects #713

Closed kapad closed 4 months ago

kapad commented 6 years ago

I have the following in my controller

@somedata = [
  {:key1 => 'hello', :key2 => 'world'},
  {:key1 => 'bye', :key2 => 'world'},
  {:key1 => 'greetings', :key2 => 'universe'},
]

and the following in my template

collection @somedata
attributes :key1, :key2

From what I understand, this should give me the following json

[
  {"key1": "hello", "key2": "world"},
  {"key1": "bye", "key2": "world"},
  {"key1": "greetings", "key2": "universe"}
]

but what I do get in the json is

[
  {},
  {},
  {}
]
kapad commented 6 years ago

Found the fix, but not really sure if it's a hack or a fix. the view file should be

collection @somedata
node do |some_item|
  attributes :key1 => some_item[:key1],
    :key2 => some_item[:key2]
end