我有一个包含父对象和子类对象的提要,我使用RABL将其呈现到json中。我在json中包含了根对象。
事情就是这个=>根据首先出现的情况,将使用相应的类生成json中的根节点。
我需要它成为所有父类。
以下示例:
Class Klass
...
Class SubclassOfKlass < Klass
...
k = Klass.create
s = SubclassOfKlass.create
array1 = [k,s]
Rabl::Renderer.json(nil, 'answers/list', view_path: 'app/views', locals: {object: array1})
## results will render everything as "Klass" objects
## [{\"klass\":{\"_id\":\"1\"},{\"klass\":{\"_id\":\"2\"}]
array2 = [s,k]
Rabl::Renderer.json(nil, 'answers/list', view_path: 'app/views', locals: {object: array1})
## results will render everything as "SubclassOfKlass" objects
## [{\"subclass_of_klass\":{\"_id\":\"1\"},{\"subclass_of_klass\":{\"_id\":\"2\"}]
### But I need the root to be always "klass" and not "subclass_of_klass"
Rabl如下(非常简单)
# list.rabl
collection @answers
extends "answers/show"
#show.rabl
if @user
node(:context_string) { |a| a[:context_string]}
node(:notebook) do |a|
if a.class == SubclassOfKlass
a.notebook
end
end
end
extends "klasses/cache"
答案 0 :(得分:3)
好的,答案结果相当简单:
collection @answers, :object_root => "answer"