访问RABL模板中的子实例 [英] Accessing the child instance in a RABL template

查看:89
本文介绍了访问RABL模板中的子实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的RABL模板

I have a RABL template as shown below

object @user
attributes :name
child :contacts do
  # does not work
  if contact.is_foo?
    attributes :a1, :a2
  else
    attributes :a3, :a4
  end
end

如何访问模板child块中的Contact对象?我需要在子实例上执行一些条件逻辑.

How do I access the Contact object in the child block of the template? I need to perform some conditional logic on the child instance.

推荐答案

您可以通过声明block参数来访问当前对象.

You can access the current object by declaring the block parameter.

object @user
attributes :name
child :contacts do |contact|
  if contact.is_foo?
    attributes :a1, :a2
  else
    attributes :a3, :a4
  end
end

旧答案

我最终使用了root_object 方法,它会在给定的上下文中返回数据对象.

Old answer

I ended up using the root_object method, which returns the data object in a given context.

object @user
attributes :name
child :contacts do
  if root_object.is_foo?
    attributes :a1, :a2
  else
    attributes :a3, :a4
  end
end

这篇关于访问RABL模板中的子实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆