2

我有一个包含父类和子类对象的提要,我使用 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"
4

1 回答 1

3

好的,答案很简单:

collection @answers, :object_root => "answer"
于 2013-03-27T06:42:31.410 回答