首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从呈现器中筛选出属性/集合?

如何从呈现器中筛选出属性/集合?
EN

Stack Overflow用户
提问于 2013-09-02 15:37:24
回答 1查看 1.1K关注 0票数 1

例如,我有一个代表,如下所示:

代码语言:javascript
复制
module AccountRepresenter

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

  property :id
  property :name

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

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

代码语言:javascript
复制
respond_with @accounts, represent_with: AccountsRepresenter

对于另一个动作,我不希望collection :assets出现。我试过了

代码语言:javascript
复制
respond_with @accounts, represent_with: AccountsRepresenter, exclude: :assets

但不起作用。我应该如何将争论传递给陈述者?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-10 21:23:48

Roar使用的是下面可表示的(https://github.com/apotonick/representable#conditions)。您可以执行以下操作以具有条件功能:

代码语言:javascript
复制
respond_with @accounts, represent_with: AccountsRepresenter, exclude: [:assets]

然后

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18576742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档