1

例如,我有一个代表看起来像:

module AccountRepresenter

  include Roar::Representer::JSON
  include Roar::Representer::Feature::Hypermedia

  property :id
  property :name

  collection :assets, :extend => AssetRepresenter, :class => Asset
end

在控制器中,我将其用作:

respond_with @accounts, represent_with: AccountsRepresenter

对于另一个我不想collection :assets被呈现的动作。我试过了

respond_with @accounts, represent_with: AccountsRepresenter, exclude: :assets

但它不起作用。我应该如何将参数传递给代表?

4

1 回答 1

2

Roar gem 在下面使用 Representable ( https://github.com/apotonick/representable#conditions )。您可以执行以下操作来获得条件功能:

respond_with @accounts, represent_with: AccountsRepresenter, exclude: [:assets]

然后

module AccountRepresenter

  include Roar::Representer::JSON
  include Roar::Representer::Feature::Hypermedia

  property :id
  property :name

  collection :assets, extend: AssetRepresenter, if: lambda { |opts| !opts[:exclude].includes?(:assets)] }
end
于 2013-12-10T21:23:48.553 回答